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 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.

share|improve this question

1 Answer 1

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

<?php
$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
share|improve this answer
    
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 at 19:00
    
sorry i mean postdata not "postdate" ;) –  Loki Jun 23 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 at 4:19

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.