1

I am trying to use WordPress' built in function for calling a list of categories. Unfortunately there doesn't appear to be an argument for how to pull the most popular categories (say top 5). I am using the code below which is pulling in all categories. Any idea on how I could modify this to display the top 5 most used categories?

<?php 
    $args = array(
    'show_option_all'    => '',
    'orderby'            => 'name',
    'order'              => 'ASC',
    'style'              => 'list',
    'show_count'         => 0,
    'hide_empty'         => 1,
    'use_desc_for_title' => 1,
    'child_of'           => 0,
    'feed'               => '',
    'feed_type'          => '',
    'feed_image'         => '',
    'exclude'            => '1',
    'exclude_tree'       => '',
    'include'            => '',
    'hierarchical'       => 1,
    'title_li'           => __( '' ),
    'show_option_none'   => __( '' ),
    'number'             => 5, // limits the number of displayed categories
    'echo'               => 1,
    'depth'              => 0,
    'current_category'   => 0,
    'pad_counts'         => 0,
    'taxonomy'           => 'category',
    'walker'             => null,
    );
    wp_list_categories( $args );
?>

1 Answer 1

3

You can order by count to order them by the number of posts they are attached to. Use number to limit the number of terms returned:

wp_list_categories( [
    'orderby' => 'count',
    'order' => 'DESC',
    'number' => 5
] );
3
  • Wow I am feeling very humbled right now. I looked at the WP documentation for this function and missed this. Thank you!
    – Peter
    Commented Oct 10, 2018 at 3:14
  • Don't feel so bad, I don't think count is listed anywhere as a valid value for orderby, you have to look at the documentation for the generic get_terms to see that it's possible to do this.
    – Milo
    Commented Oct 10, 2018 at 3:23
  • Just want to throw in that unsurprisingly this query string also works for get_categories()/get_terms()! Thanks so much for this solution, I had no idea it was possible! $terms = get_categories(array('orderby' => 'count', 'order' => 'DESC', 'number' => 10));
    – jerclarke
    Commented Mar 20 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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