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 );
?>
2

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
] );
  • Wow I am feeling very humbled right now. I looked at the WP documentation for this function and missed this. Thank you! – Peter Oct 10 '18 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 Oct 10 '18 at 3:23

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.