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/
Related Posts:
- How to show list of posts by author and category?
- Count posts in category including child categories
- Authors in menu, template list post by author
- Show the author’s count of total amount of posts assign to a custom taxonomy term
- How can I list random authors from current post category?
- Count to how many categories a post belongs to
- Display Notification Bar on Header on Certain Post Count
- Changing the Category for all posts of an Author
- Get Posts Under Custom Taxonomy
- How to mark every 3rd post
- How to display related posts from same category?
- How to show related posts by category
- Count posts within a custom post type and specific taxonomy and terms?
- How to add category to: ‘wp-admin/post-new.php’?
- How to Display a List of Users Who Have Made at Least 1 Post?
- Including categories in search results
- Use category base slug in posts’ permalink
- Hook that get’s triggered when the author of a post is changed
- Different post sort order within different categories
- the_author() returns empty string
- Count how many posts in category
- Prevent Authors from viewing each others Posts
- Custom Single Post By Category
- How to Display Network Post Count?
- Setting Custom Sort Order of Posts within a Category
- Display Random Author with Details in Sidebar
- How to get the number of posts in a selected category?
- MySQL Query to Retrieve Category from wp_posts
- Different number of posts in each category
- Display all posts from specific categories on a page
- Display the latest post from a category in a page
- WordPress: How to get the current logged in author page URL?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- How to cache a shortcode functions output?
- Category checkbox list tree changes when editing a post
- How to add editor’s name to entry meta byline?
- Different permalink for posts and authors
- How to add metabox for post of specific category
- Convert custom fields to post categories
- Manipulate post category after time
- Show related posts by category but ignore one category
- How to sort posts by last name (2nd word) on ONE category only?
- How to disable Uncategorized category URL?
- Add role that restricts user to post in specific category
- How to get Category Id from Post Id ?
- How to display posts by current user/author in a custom page template?
- Query posts from current year
- Getting Position of a post in a category
- Display only posts from a specific parent category
- How to control which category will be picked for the slug of a post?
- Categories box not showing on post edit pages
- Query posts distinct authors
- get_the_term_list by hierarchy order
- How to change the layout and styling of posts according to their category in WordPress?
- Restrict the Number of Posts an Author can Publish (over time)?
- Search posts by post author name
- Get the user type of an author
- Count singular post views automatically
- Show the title of the latest post by author
- List only child categories a post is in, of a specific parent category
- Add default content to post (for specific category)
- Find most recent authors
- Query only Posts from Both of Two Category?
- Show Custom Taxonomy Categories, Listing of Posts, and Single Post via AJAX
- Add custom field to all posts in specific category
- Exclude category on blog list page
- Show author post count in sidebar – Variable
- Set font size automatically according to number of words in post
- Get current users post URL?
- Get the author of the latest revision
- Filter posts by author and category simultaneously
- display sub categories assoccited with each post in a category archive page
- is there a better way of combining this?
- Display most recent post in category instead of archive?
- Custom Post Type & Author not associating, user post count is 0, api doesn’t return author in post objects
- Guest Author – How to modify my custom function code if the guest author URL will follow a particular pattern/format?
- How can I specify a category post on my home page
- Network wide post count (WP Multisite)
- Single.php Active Category Class
- Most efficient way to list all categories and display a post for each of them?
- Archived Posts in Wrong Category
- Show One Category on Post
- Redirect based on user post count
- Use template of parent category for single post sub-categories
- prevent showing posts of an specific category in admin posts section
- How to choose a sort order (for posts) per category? (ideally when creating a new category)
- Post author is changed to admin after his post is modified by admin
- wp_list_categories link to first post of category instead of calling taxonomy template
- Alternate custom content in the loop
- How to search only by post title and category?
- Add custom field to category of custom post type
- WordPress Child Category Display All Posts
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- Get all posts by post_author
- get_post_type_archive_link(‘post’) returns current category archive
- Display all posts under child sub category in sidebar on post page?
- Do I use custom post type or something else?
- Show that current post is number X out of X
- Can I hide a specific post from latest posts page?
- How do I get the slug of a custom taxonomy category of a post?