I want to count authors post count in a specific category. How do i do that?
I have red this thread here but still can’t fiure it out.
Count number of posts by author in a category
Edit: This is what i got and tried but doesnt work at all.
$user_id = get_the_author_meta('ID')
$args = array(
'author_name' => $user_id,
'category_name' => 'categoryname',
};
$wp_query = new WP_Query($args);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo $my_count = $wp_query->post_count;
wp_reset_postdata();
endwhile;
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You don’t need to loop through the posts for this.
Instead, just call the method get_posts() which will return an array of posts and then just count the number of posts in that array.
$user_id = get_the_author_meta('ID');
$args = array(
'author_name' => $user_id,
'category_name' => 'categoryname',
);
$wp_query = new WP_Query($args);
$posts = $wp_query->get_posts();
$my_count = count( $posts );
echo $my_count;
Reference
https://developer.wordpress.org/reference/classes/wp_query/get_posts/
Method 2
Here is the correct code. It shows authors post count from a specific category by category slug name.
$user_id = get_the_author_meta('ID');
$args = array(
'author' => $user_id,
'category_name' => 'category_slug_name',
);
$my_query = new WP_Query( $args );
$my_count = $my_query->post_count;
echo $my_count;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0