If you just want to count the total number of posts that matched a specific query, then there’s no need to set the posts_per_page
to -1
. Just set it to 1
instead.
And $count_posts->count
returns null
because the WP_Query
class doesn’t have a property named $count
, i.e. WP_Query::$count
does not exist.
WP_Query
does have $post_count
property, but that is just the number of posts displayed per page, so you would not use it in your case. Instead, what you would use is the $found_posts
property which is the total number of posts found for the query.
So instead of $posts_per_cat = $count_posts->count;
, use $posts_per_cat = $count_posts->found_posts;
.
And you can check all the properties in WP_Query
on this page.
Related Posts:
- How to filter categories by custom field?
- How do I count how many top level categories there are?
- How to get the number of child categories a specific parent category has?
- Warning: sizeof (): Parameter must be an array or an object that implements Countable, On products pages
- Pagination is not working properly in Product Category/Tag pages
- There any way to get de percentage of number of post by user and category?
- Get Number of total Parent Categories for Custom Taxonomy
- Single.php Category Post Count
- If x comments in a post move post to category y
- Count tags for a specific category
- Show the amount of posts in a tag in a specific category has
- Combining a meta_query with a category filter
- Change categories count to include media attachments
- Show category meta in loop on homepage
- How to exclude posts from category posts count
- Show the number of the post
- Count how many posts have a specified tag AND category
- Get_term_meta() does not work with pre_get_posts()
- Add subcategories posts to the counts column at the admin’s categories list
- Category count but only if category has at least one post
- How to make child categories recognize parent’s template displays
- Completely disable categories
- get_field not displaying my custom field value
- Show div only if post is in specific category
- wp_list_categories exclude not working
- How can I only show certain posts?
- Category menu item and its last 10 posts as sub-menu
- what is the action hook code to supporting product category condition in single product page of woocommerce?
- Prevent function from triggering again when post in specific category gets another category?
- Posts from multiblt categories but featured image only in the first one, the rest to have the post title only
- Conditional Tag for sub cagtegories?
- Display only posts from referred category on date archive page
- Sort post’s categories by ID
- Add a wordpress blog to my website having users
- in_category() not working in WordPress 4.9.7
- Show Featured products in product category pages
- How to remove category titles?
- Inserting Category programmatically
- Category-slug.php not being used
- Get only one of the current categories
- Show Available Taxonomy List with Current Category
- How to get grandparent of a given category
- Additional form options based on category selected not working
- Pagination for category slug returns 404 when page >= 2
- Display category-specific comments in sidebar
- Why does the argument list_only do on Walker_Category_Checklist::start_el?
- Paging not working in category.php
- Create menu / submenu from category subcategory and posts
- Display list of categories filtered by date?
- Redirect single article permalink to paginated category page
- Exclude category by slug in pre_get_posts?
- add slug beside name in Admin Category Checklists
- How to show 5 posts from specific category on related page?
- List only categories i specify
- Author Page – List of categories plus number of posts in that category
- Exclude category from related post
- On the Blogpost I have categories showing, is there a way to exclude a catgegory like ‘Media’?
- How can you access category information from a theme?
- Filter WooCommerce archive pages by an additional category
- Posts from a category on homepage with category archieves page default css
- How to use wp_list_categories with plugin category?
- Theme modification for custom category menus
- Custom css per category and per single post belong in each category
- How Do I Delete WordPress Posts Older Than 400 Days, From A WordPress Category
- Category Page Template – Can’t get category ID
- Filter Category Loop Dynamically
- Subcategory is displayed before Parent Category
- Shortcode to display all the post from a category in a page
- WP Category Meta plugin fix? [closed]
- How to display WooCommerce category image in my category page banner as a background?
- Order wp_dropdown_categories by ASC or DESC
- how tho change number of posts in loop from specific categories
- Showing the category hierarchy as clicked in WordPress, with image and desc
- error in specific category loop
- WordPress category title not update in navigation menu
- problem with filter of custom portfolio categories
- Deleted categories still listed until additional page refresh
- Removing catagory for new blog pages
- Limit post top level categories to one
- “Next posts” of a category do not call category.php
- Trying to display all posts in a category
- Remove “Category:”
- List post categories that link to a page showing those posts
- How to count the number of monthly archive?
- Most efficient use of custom taxonomies, categories, and pages
- AZ Directory category
- Pluck single parent category from get_the_category
- Removing short versions of category archive URL
- Custom Category Page Not Working [duplicate]
- Display post from specific category
- Display one result for category post type
- Return selected categories only in custom page meta box?
- In a url where Category Id is wrong It is showing wrong it is going to the page not found page
- how wordpress generates permalinks as /%category%/%post%/
- How to make that all posts with a category belong also to another
- Featured image for links and categories
- get_category_children/ the new get_term_children not work
- wp_list_categories() not listing categories
- List the category tree of all the product_cat categories
- Is there a function to modify existing previous/next post link without modifying theme files?