0

What is the optimal method for getting the number of posts by an author in a category? I'm not looking to get the posts themselves, just a count of them.

count_user_posts returns a count of all the posts of a user in a post type, but there is no option to filter by category. Any ideas would be much appreciated. Thanks!

4
  • In one specific category or every category the author has written a post under?
    – Cedon
    Commented Mar 24, 2017 at 23:05
  • In one specific category.
    – Tom
    Commented Mar 24, 2017 at 23:06
  • How are you identifying the category? By ID? Slug? etc. Same with Author
    – Cedon
    Commented Mar 24, 2017 at 23:08
  • Identifying both by ID
    – Tom
    Commented Mar 24, 2017 at 23:12

1 Answer 1

1

You could do this by creating a custom WP_Query and then counting it.

$args = array( 
   'author' => 1,
   'cat'    => 5,
);

$my_query = new WP_Query( $args );
$my_count = $my_query->post_count;

Just change 1 and 5 to the Author ID and Category ID respectively.

Alternatively you can use the category slug or author's nice name (NOT name!) as well:

$args = array(
   'author_name'   => 'bob',
   'category_name' => 'editorial',
};

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.