• jeremyschultz

    (@jeremyschultz)


    I’m attempting to get a set of posts with a wp_query, and if it returns less than two I make a second wp_query and merge the results. In my test case, I get one result on query #1 and two on query #2 but in all cases the combined query #3 is empty.

    <?php $query = new wp_query( array ( 'posts_per_page' => '2', 'orderby' => 'date', 'category_name' => 'as,ac,ai', 'cat' => '96,-31,-98', 'post__not_in' => array(718) ) );
    if (($query -> post_count) < 2) {
    	$query2 = new wp_query( array ( 'posts_per_page' => '2', 'orderby' => 'date', 'category_name' => 'as,ac,ai', 'cat' => '-96,-31,-98', 'post__not_in' => array(718) ) );
    	//echo $query -> post_count; returns 1
    	//echo $query2 -> post_count; returns 2
    	$query3 = array_merge($query, $query2);
    	//echo "query3 post count is " . $query3 -> post_count; returns blank
    	//print_r($query3); nothing
    }

    I’ve tried everything I can think of and nothing returns anything. What am I missing?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    When you create a new WP_Query, you are instantiating a class object, not an array. You cannot merge objects. The actual posts array is $query->posts, try merging those.

Viewing 1 replies (of 1 total)
  • The topic ‘Merging post wp_queries with array_merge not working’ is closed to new replies.