0

I have used register_taxonomy() with 'show_admin_column' => true successfully to get a drop-down on my custom post type listing page. The drop-down displays the terms in ID order by default.

Anyone know a way to change this to Alpha order?

  • 'show_admin_column' => true can not add the drop-down by itself. It adds only a column to the List Table. What code did you use to create the drop-down? – Max Yudin Aug 3 '17 at 16:09
0

@Max - good call! Did some more digging and found a wp_dropdown_categories embedded in a function where I could use 'orderby'.

0

If somebody is facing the same problem, here is the snippet:

// 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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.