简体   繁体   中英

Iterating over correct number of wordpress posts from category + custom post type

Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?

<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
    <div id="content">
        <?php while ( have_posts() ) : the_post(); ?>
            <?php the_title( '<h1>', '</h1>' ); ?>
            <?php the_post_thumbnail( 'full' ); ?>
            <?php the_content(); ?>
            <?php if ( $cats = get_field( 'category' ) ) : ?>
                <?php
                    $args = array(
                        'post_type' => array( 'post' ),
                        'category__in' => $cats,
                        'fields' => 'ids',
                    );

                    $articles = new WP_Query( $args );
                    wp_reset_postdata();

                    $args = array(
                        'post_type' => array( 'case_study' ),
                        'fields' => 'ids',
                        );
                    $case_study = new WP_Query( $args );
                    wp_reset_postdata();

                    $all_posts_ids = array_merge( $articles->posts, $case_study->posts );

                    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                    $args = array(
                        'post_type' => array( 'post', 'case_study' ),
                        'post__in' => $all_posts_ids,
                        'paged' => $paged,
                        );                  

                    query_posts( $args );
                ?>
                <?php if ( have_posts() ) : ?>
                    <?php while ( have_posts() ) : the_post(); ?>
                        <?php get_template_part( 'blocks/content', get_post_type() ); ?>
                    <?php endwhile; ?>
                    <?php get_template_part( 'blocks/pager' ); ?>
                <?php else: ?>
                    <?php get_template_part( 'blocks/not_found' ); ?>
                <?php endif; wp_reset_query(); ?>

            <?php endif; ?>
        <?php endwhile; ?>
    </div>
    <?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>

There is a number of things you're doing wrong.

You are using a page template, and then are looping using the loop . This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here ).

Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).

And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.

So the plan of attack should be:

  1. What do I want to show?

  2. How and where do I want to show it?

  3. What do I need to do to achieve this?

  4. Start writing things needed to achieve this.

  5. ???

  6. Profit!!!

If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).

Hope this helps.

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