简体   繁体   中英

Wordpress - Category not showing all posts

        <?php
            $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1));

            if ( $wpb_all_query->have_posts() ) : ?>

            <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
                $cats = get_the_category(); ?>
                <?php if ($cats[0]->cat_name === 'Coaching') { ?>
                <div class="callout horizontal">

                    <?php the_post_thumbnail(); ?>

                    <div class="content">
                        <h5><?php the_title(); ?></h5>
                        <?php the_content(); ?>
                    </div>

                </div>
                <?php } ?>
            <?php endwhile; ?>

        <?php endif; ?>

The above code is meant to only show categories that are from the category Coaching. However this happens for two of my category posts but not for the other three that are labeled under Category.

It is worth noting that I have set my reading posts in the wordpress settings to 9999 just incase this was a pagination problem.

Can anyone see what the error might be.

I would like to also mention that this logic was working not so long ago before I added a lot more posts.

Cheers

************** UPDATE ***************

To add to the mystery I have noticed that this is happening on another part of the site. Thats isn't related to the posts. But pulling out from Advanced Custom Fields.

EDIT:

I've seen now that your post query will return all posts, and you're checking for category inside the loop, which is kinda redundant. Modify your query like this:

$wpb_all_query = new WP_Query( 
                    array(
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'category_name' => 'coaching', //slug, not name!!
                        'posts_per_page' => 9999
                    )
                );

This will pull only posts from that category. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM