Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Example:

News (5 new)
Articles (8 new)

...and so forth.

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

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

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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