有没有办法在Custom Post Listing页面上重新排序Custom Taxonomy下拉菜单?

时间:2017-08-03 作者:troda21

我用过register_taxonomy() 具有\'show_admin_column\' => true 成功获取自定义帖子类型列表页面上的下拉列表。默认情况下,下拉列表按ID顺序显示术语。

有人知道把这个改成字母顺序的方法吗?

2 个回复
SO网友:troda21

@Max-打得好!做了一些进一步的挖掘,发现了一个嵌入在函数中的wp\\u dropdown\\u categories,我可以在其中使用“orderby”。

SO网友:Max Yudin

如果有人面临同样的问题,下面是一个片段:

// Based on the User Contributed Notes of the Codex
add_action(\'restrict_manage_posts\',\'my_post_type_filter\', 10, 2);

function my_post_type_filter($post_type, $which){
    if(\'faq\' !== $post_type){
        return; // check the post type
    }
    $taxonomy_slug = \'faq_cat\';  // subject to change
    $taxonomy = get_taxonomy($taxonomy_slug);
    $request_attr = \'faq_cat\'; // subject to change, will show up in the url
    $selected = \'\';
    if ( isset($_REQUEST[$request_attr] ) ) {
        $selected = $_REQUEST[$request_attr]; // in case the current page is already filtered
    }

    wp_dropdown_categories(array(
        \'show_option_all\' =>  __("Show All {$taxonomy->label}"),
        \'taxonomy\'        =>  $taxonomy_slug,
        \'name\'            =>  $request_attr,
        \'orderby\'         =>  \'name\',
        \'order\'           =>  \'ASC\',
        \'hierarchical\'    =>  true,
        \'depth\'           =>  1,
        \'show_count\'      =>  true, // Show number of post in parent term
        \'hide_empty\'      =>  false, // Don\'t show posts w/o terms
        \'selected\'        =>  $selected,
    ));
  }

/**
 * Filter posts by taxonomy in admin
 * @author  Mike Hemberger
 * @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
 */
add_filter(\'parse_query\', \'tsm_convert_id_to_term_in_query\');

function tsm_convert_id_to_term_in_query($query) {
    global $pagenow;
    $post_type = \'faq\'; // subject to change
    $taxonomy  = \'faq_cat\'; // subject to change
    $q_vars    = &$query->query_vars;
    if ( $pagenow == \'edit.php\' && isset($q_vars[\'post_type\']) && $q_vars[\'post_type\'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
        $term = get_term_by(\'id\', $q_vars[$taxonomy], $taxonomy);
        $q_vars[$taxonomy] = $term->slug;
    }
}

结束

相关推荐