简体   繁体   中英

Sort Category/taxonomy based on post count in wordpress

Is there a way to sort category/taxonomy list based on number of posts it contains?

regards, Desizner

<?php    
    foreach (get_categories('orderby=count&order=DESC') as $category ) 
    {
    /*Some stuff here*/
    }

?>

For more details take a look at: https://developer.wordpress.org/reference/functions/get_categories/

Currently (September 2017) I also do it using wp_list_categories (as Ryan B) but with the following code:

<?php wp_list_categories( array(
    'orderby'    => 'count',
    'order'      => 'DESC'
) ); ?>

Yes there is, see wp_list_category

<?php wp_list_category('orderby=count'); ?>

is what you would use

You can use get_categories() function and pass one of these values in 'taxonomy': 'category' (to get only categories) or 'post_tag' (to get only tags) or even remove this key and it'll get both. Bellow we're ordering by posts counting on each category DESC.

<?php

$categories = get_categories([
    'taxonomy' => 'category',
    'orderby'  => 'count',
    'order'    => 'DESC'
]);

foreach ($categories as $category) {
   // Do something
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM