如何在admin meta-box下拉菜单中按分类术语过滤CPT的帖子?

时间:2016-03-10 作者:NickC

我一直在疯狂地寻找解决办法。我试图在一侧的元框中生成两个下拉字段。

第一个下拉字段应该由所选分类法的术语填充。

第二个应该由和基于该选择的所选分类术语相关联的帖子填充。

例如

假设我有分类法“property type”,术语“flat”或“house”,3个贴子与“houses”关联,7个贴子与“flat”关联。我怎样才能得到一个双下拉列表,其中一个选择“公寓”(或房子),并从中关联的帖子出现在下一个下拉列表中?

1 个回复
SO网友:majick

这个问题比你想象的要复杂得多。

尽管如此(或者真的,因为它),我还是要尝试一下。。。

add_metabox(\'terms-post-selector\',\'Select Related Post\',\'post_terms_selector_dropdown\',null,\'post\',\'side\',\'high\');

function post_terms_selector_dropdown() {

    global $post;
    $taxonomy = \'property_type\'; // or whatever it is
    $terms = get_the_terms($postid,$taxonomy);
    $selectedterm = get_post_meta($post->ID,\'_selected_term\',true);

    if (count($terms) > 0) {
        echo "<select name=\'termselector\' id=\'termselector\' onchange=\'loadpostterms();\'>";
        foreach ($terms as $term) {
            echo "<option value=\'".$term->value."\'";
            if ($term->value == $selectedterm) {echo " selected=\'selected\'";}
            echo ">".$term->name."</option>";
        }
        echo "</select>";

        echo "<div id=\'posttermselecter\'></div>";
    }

    // javascript to trigger the AJAX creation of post dropdown select element
    echo "<script>function loadpostterms() {
        var ajaxurl = \'".admin_url(\'admin-ajax.php\')."\';
        var termselector = document.getElementById(\'termselector\');
        var selectedterm = termsselector.options[termselector.selectedIndex].value;
        document.getElementByID(\'termqueryiframe\').src = ajaxurl+\'?action=post_terms_dropdown&term=\'+selectedterm+\'&postid=".$post->ID."\';
    </script>";

    // just a dummy iframe to handle the AJAX post selector callback
    echo "<iframe src=\'javascript:void(0);\' id=\'termqueryiframe\' style=\'display:none;\'></iframe>";

}


// AJAX callback to populate the post select element
add_action(\'wp_ajax_post_terms_dropdown\',\'post_terms_dropdown\');
add_action(\'wp_ajax_nopriv_post_terms_dropdown\',\'post_terms_dropdown\');
function post_terms_dropdown() {

    // get all posts with specified term
    $term = $_GET[\'term\'];
    $taxonomy = \'property_type\'; // or whatever it is
    $args = array(\'tax_query\' => array(array(
    \'taxonomy\' => $taxonomy, \'field\' => \'ID\', \'terms\' => $term
    ) ) );
    $posts = get_posts($args);

    if (count($posts) > 0) {
        // get the currently selected post
        $postid = $_GET{\'postid\'];
        $selectedpost = get_post_meta($postid,\'_selected_post\',true);

        // create the post dropdown and send it back to parent frame
        echo "<script>var postselector = \'<select name=\\"posttermselector\\">\';";
        foreach ($posts as $post) {
            echo "postselector += \'<option value=\\"".$post->ID."\\"";
            if ($post->ID == $selectedpost) {echo " selected=\\"selected\\"";}
            echo ">".$vpost->post_title."</option>\';";
        }
        echo "postselector += \'</select>\';
        parent.document.getElementById(\'posttermselector\').innerHTML = postselector;</script>";
   }
   exit;
}

// update the hidden post fields on post publish / save
add_action(\'publish_post\',\'select_post_terms_update\');
add_action(\'save_post\',\'select_post_terms_update\');

function select_post_terms_update($postid) {

    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {return;}
    if (!current_user_can(\'edit_post\',$postid)) {return;}

    if (isset($_POST[\'termselector\'])) {
        update_post_meta($postid,\'_selected_term\',$_POST[\'termselector\']);
    }
    if (isset($_POST[\'posttermselector\'])) {
        update_post_meta($postid,\'_selected_post\',$_POST[\'posttermselector\']);
    }

}
请注意,这个网络的政策并不是像我在这里所做的那样鼓励他人“为你做你的工作”——我只是想做一些有趣的事情,这个问题问得很好。祝你好运,我希望它对你有用!(没有像这样从头开始写的担保。)

相关推荐

如何使用GET_POSTS根据单个帖子ID进行过滤?

如何使用get\\u posts根据单个post id进行筛选?(我不想使用get\\u post,因为我在另一个函数中使用它,该函数可能会使用其他参数,并且应该返回一个数组)。以下方法不起作用:get_posts(array( \'ID\' => 12345, )); get_posts(array( \'p\' => 12345, )); get_posts(array( \'post\'