1

I have the following code to merge 3 different queries:

<div class="features-list">
            <?php

                $args1 = array (
            'post_type' => 'page',
            'post__not_in' => array(2, 48, 50, 52, 54, 56, 61, 116, 129, 58665, 58666, 58667, 58668, 57800, 62883, 74802, 75597, 75599, 75601, 75609),
            'orderby' => 'date',
            'order' => 'DESC',
                    'meta_key' => '_wp_page_template',
                    'meta_value' => 'template-features.php'
                );
            $query1 = new WP_Query( $args1 );

            // If there are results, then push the IDs into an array
            $query1_posts = array();
            if ( $query1->have_posts() ) { 
              while( $query1->have_posts() ) {
                $query1->the_post();
                array_push( $query1_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

                $args2 = array (
            'post_type' => 'page',
            'orderby' => 'date',
            'order' => 'DESC',
            'meta_key' => 'feature_type',
            'meta_value' => 'guide'
                );
            $query2 = new WP_Query( $args2 );

            // If there are results, then push the IDs into an array
            $query2_posts = array();
            if ( $query2->have_posts() ) { 
              while( $query2->have_posts() ) {
                $query2->the_post();
                array_push( $query2_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

                $args3 = array (
            'post_type' => 'new_features',
            'orderby' => 'date',
            'order' => 'DESC',
            'meta_key' => 'feature_type',
            'meta_value' => 'guide'
                );
            $query3 = new WP_Query( $args3 );

            // If there are results, then push the IDs into an array
            $query3_posts = array();
            if ( $query3->have_posts() ) { 
              while( $query3->have_posts() ) {
                $query3->the_post();
                array_push( $query3_posts, get_the_ID() );
              }
            }
            wp_reset_postdata();

            $results = array_unique ( array_merge( $query1_posts, $query2_posts, $query3_posts ) );

            // WP_Query arguments
            $the_args = array (
              'post__in' => $results
            );

            // The Query
            $the_query = new WP_Query( $the_args );
                $count = 1;

                if ( $the_query->have_posts() ) { 
                while ($the_query -> have_posts()) {
                    $the_query -> the_post();
            ?>
            <article class="<?php if ($count == 1) { ?>g1_2<? } else { ?>g1_4<?php } ?>">
                <div class="feature_image">
                    <a href="<?php the_permalink(); ?>"><?php if ($count == 1) { the_post_thumbnail('full'); } else { the_post_thumbnail('thumbnail'); } ?></a>
                    <div class="feature_title"><a href="<?php the_permalink(); ?>"><?php the_title();?></a></div>
                </div>
                <p><?php the_excerpt();?></p>
            </article>
            <?php
                        $count++;
                    }
                }
                wp_reset_postdata();
            ?>
        </div><!-- .features-list -->

I know that $results has all the post IDs I need, but still the $the_query returns no posts.

What am I missing. I rewrote this code at least 10 times by now.

2
  • What's the output of var_dump( $results ) ?
    – birgire
    Jun 13, 2015 at 10:50
  • Sorry for the late reply. The output contained the array of posts, that's why I was lost. The answer accepted below fixed my problem.
    – Reddo
    Jun 13, 2015 at 16:22

1 Answer 1

3

you might also need to set the 'post_type' in $the_args, and possibly the 'posts_per_page'; http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

example:

    // WP_Query arguments
    $the_args = array (
      'post__in' => $results,
  'post_type' => array( 'any' ),
  'posts_per_page' => 10
    );
1
  • This seemed to work. At least the posts are showing.Thanks a lot for your help!
    – Reddo
    Jun 13, 2015 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.