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
- Prevent Authors from viewing each others Posts
- How to get the number of posts in a selected category?
- How to cache a shortcode functions output?
- Show related posts by category but ignore one category
- Query posts distinct authors
- Restrict the Number of Posts an Author can Publish (over time)?
- Count singular post views automatically
- 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?
- Add custom field to category of custom post type
- Can I hide a specific post from latest posts page?
- Multiple authors for single post without plugin
- How to show post(excerpt) from specific category on wp page?
- WordPress – Admin Manage Posts – Multiple Filters by Parent Category
- Author must complete profile info before they can publish a post?
- Custom Theme Want To Remove One Category From All Displays
- WordPress menu that shows all posts in a certain category like w3schools
- Divide Loop Into Days & Categories
- Different template for posts of all subcategories of category
- How do I render all posts of the same category in same layout?
- How to limit displayed posts on wordpress
- Mark menu item as current-menu-item for category
- How to set up multiple taxonomies for groups of posts
- How can I show wordpress posts based on author?
- How to display category from recent posts?
- Can I divide the pages into categories?
- How to give capability (publish contributors posts) to author role?
- Duplicate posts and change category
- Category Foreach keeps looping?
- display a random post thumbnail from a random category
- Post page to display specific category
- how to show single post in a custom template
- Excluding posts not working
- How to add category post count in main navigation menu [closed]
- Limit the number of posts a category can have – newest post goes in, oldest one drops out, possible? plugin?
- (solved) getting post author’s user role
- Remove current category from post but display all others
- Counter is skipping post when I still want it visible [closed]
- How do I show posts from another wordpress installation?
- Latest posts by category — how to exclude current post?
- How do I include the category next to the title of a post?
- How to show a custom taxonomy in the theme?
- How to import nested categories from XML file?
- Migrate posts from category and sub-category via SQL
- Posts Missing in Dashboard after update
- Display post number by category
- Restrict displaying posts to the poster itself (in Back-end)
- How to get post with slug and exclude categories
- How can I set a maximum allowed post size and number of posts submitted?
- Get x recent posts by author?
- Retrieving posts by their date and category
- Are post, page and category IDs unique to each other?
- Static pages not working
- WordPress displays post on subcategory only
- Remove Featured Image from posts in specific category?
- Author Avatar as default first image
- Prevent 404 of Author pages without posts
- Categories Listing and Highlighting current category item
- Display hierarchical categories/sub-categories for a single post only
- Post in bulk based on category template
- On the online version of my blog posts disappear
- Current post categories and subcatecories outside of the loop
- Query prints posts without specific categories
- Foreach issue in get_posts function
- Count singular post views automatically
- How to count posts in tags within time limit
- On WordPress Search, how to search post from Author meta also
- More Than 50K Categories and WordPress Admin Panel Stop Showing Categories and Posts
- Re-order Category Meta-data
- Custom post styling per category?
- Retrieve posts inside foundation tabs and tab-content split by 3 per row
- post categories – how to show only categories with a specific parent id
- Creating a “category” of featured posts
- How do I make it so that new posts within a certain category go into a certain page?
- List Authors For Current Category
- List posts of assigned categories in list of all categories on single.php
- Show only top 3 posts from 3 categories in order on home page
- Assign specific ads to specific categories/posts
- find posts with exactly 3/4 categories
- Use radio buttons & checkboxes for post categories
- Displaying only posts from a certain category on my custom page template
- WP_Query: Fetch posts that are in (category1 and not in category2), OR posts that are not in cagegory1
- Add default content to post (for specific category)
- List posts that are in the same category as the current post title
- Multiple blog “feeds” in a single blog each with distinctly styled post pages
- How to get pagination working with “Pending” Posts
- Adding Count Post in Page
- Post’s arent displaying
- Show with echo if a post has two or more categories
- How can I get on the same row two post of different categories?
- Display post count on archive page in reverse order
- Filters do not work when there are multiple (one works)