0

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.

1 Answer 1

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
3
  • 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, 2014 at 19:00
  • sorry i mean postdata not "postdate" ;)
    – Loki
    Jun 23, 2014 at 19:07
  • I did not realize the shear amount of posts per author> you should update your question with that info Jun 24, 2014 at 4:19

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.