I want to display the posts count of each author, but posts that are in some categories should be excluded from the count.

I tried doing this with count_user_posts( $user->ID ), but this function doesn't have any category excluding options. My second approach was to do this by querying the posts table with mySQL's COUNT function, but the category IDs are not stored in that table, so I can´t exclude categories this way.

Now, before I do some weird and less performant JOIN querys, I hope someone can show me a better way how to do this.


You can run a query using get_posts to retrieve posts from a specific author, and exclude certain categories, and then use count to return the amount of posts. get_posts uses the same parameters as WP_Query, so you can have a look at the parameters there

$args = array(
    'author' => 1 //ID of the author
    'posts_per_page' => -1, //Retrieve all posts
    'category' => -13, // Category to exclude, must be ID, as category is passed to WP_Query as cat
$posts_array = get_posts( $args );
echo count($posts_array); //Display post count
  • yes this works but this call has a very bad performance. I´ve some authors with over 20k posts ( automated content from other sites ). Its causing a timeout error there. I think this is because the whole postdate will be loaded with this call. – Loki Jun 23 '14 at 19:00
  • sorry i mean postdata not "postdate" ;) – Loki Jun 23 '14 at 19:07
  • I did not realize the shear amount of posts per author> you should update your question with that info – Pieter Goosen Jun 24 '14 at 4:19

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.