I'm fairly stumped on this.

Without getting too query-heavy, I was wondering what path one might take to produce a menu that links to a category of posts, and displays in parenthesis how many new posts there are.


News (5 new)
Articles (8 new)

...and so forth.

What is the most efficient way to achieve this without running a million queries?

  • 1
    What is the "new" which is appended? Do you (also) want to do some date/time diff-ing?
    – kaiser
    Jun 3, 2014 at 9:15
  • That is correct! Getting a count would be good, getting a date evaluation (new) would be better. Jun 3, 2014 at 19:07

2 Answers 2


WordPress' get_categories() function returns an array of category objects, each of which has a "count" property for the number of posts in that category. So something like this would get you a list of links to category pages:

$categories = get_categories();

$markup = '<ul>';
foreach( $categories as $category ) {
   $catName = $category->category_nicename;
   $count   = $category->count;
   $url     = get_category_link( $category->cat_ID );

   $markup .= '<li>';
     $markup .= "<a href=\"{$url}\">{$catName} ({$count})</a>";
   $markup .= '</li>';
$markup .= '</ul>';

That would allow you to show a count using a single query. I'm sure there is a way to find a count of "new" posts using a more complex custom query instead of get_categories(), but I'd probably just eat any extra overhead of making another query for each category as opposed to trying to do it all in a single query. Call me lazy.


If you call wp_list_categories, and add the parameter 'show_count'=> 1, it will return the number of posts in each category, inside parentheses.

Regarding getting a date evaluation, you might want to use the date parameters in WP_Query.

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.