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!

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

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, privacy policy and cookie policy

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