Category post count is not correct

When I go to Posts Categories, some categories show that they have a post. See screenshot. But there are no posts linked to that category. When you click on that 1-number, no posts are shown.

This is an issue because my blog page shows the category title and description even if there are no actual posts linked to that category (Wedge-system - see screenshot).

This is how I loop over all the categories and show the posts from that category:

?php
$loop = new WP_Query( array(
                            'post_type' = 'Post',
                            'posts_per_page' = $post_per_page,
                            'tax_query' = array(
                                array(
                                    'taxonomy' = 'category',
                                    'field'    = 'id',
                                    'terms'    = array( $category-term_id ),
                                    'operator' = 'IN'
                                ),
                            ),
                        )
                        );
?

?php while ( $loop-have_posts() ) : $loop-the_post(); ?
    /* post template here */
?php endwhile; wp_reset_query(); ?

Topic blog loop taxonomy categories Wordpress

Category Web


You're a post type that doesn't exist, there is no Post post type, did you mean post?


Sidenotes:

  • wp_reset_query is only for use after query_posts you should be using wp_reset_postdata instead.
  • There is no if ( $loop->have_posts() ) check so if no posts are found there is no message and it looks like the page is broken
  • This page would be much faster if you used category archives for your category archives, then used pre_get_posts to modify the query, instead of a page template and a brand new custom query
    • if you are doing this on category.php or archive.php then you're doubling the number of queries so that you can change posts_per_page, use pre_get_posts to change it instead

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.