2

I'm trying to merge 2 WP_Query in a new WP_Query and then order all posts in this new merged query by date descending. I can merge 2 WP_Query and work fine, but I can't order the new query

    $args1 = array(
             'posts_per_page' => '10',
             'post_status' => 'publish',
             'post_type' => array('news','partners'),
             'orderby' => 'date',
             'order' => 'DESC'
             );
    $queried_post1 = new WP_Query($args1);

    $args2 = array(
             'posts_per_page' => '10',
             'post_status' => 'publish',
             'post_type' => array('post'),
             'orderby' => 'date',
             'order' => 'DESC',
             'tax_query' => array(array(
                            'taxonomy' => 'tax',
                            'field' => 'id',
                            'terms' => '5'
                            ))
             );
             $queried_post2 = new WP_Query($args2);

             $merged_queried_post = new WP_Query();
             $merged_queried_post->posts = array_merge($queried_post1->posts,$queried_post2->posts);
             $merged_queried_post->post_count = $queried_post1->post_count + $queried_post2->post_count;

In this point, I need to order all posts in $merged_queried_post by date.

Thanks for help

1
  • 2
    Why are you trying to merge these 2 queries? Why not just write a new query based off the first?
    – Howdy_McGee
    Feb 24, 2014 at 20:55

2 Answers 2

11

A better approach could be using three queries. First two query retrieve the post ids, and third one query post by ids.

// first query
$first_ids = get_posts( array(
    'fields'         => 'ids',
    'posts_per_page' => '10',
    'post_status'    => 'publish',
    'post_type'      => array('news','partners'),
    'orderby'        => 'date',
    'order'          => 'DESC'
));

// second query
$second_ids = get_posts( array(
    'fields'         => 'ids',
    'posts_per_page' => '10',
    'post_status'    => 'publish',
    'post_type'      => array('post'),
    'orderby'        => 'date',
    'order'          => 'DESC',
    'tax_query'      => array(array(
        'taxonomy'       => 'tax',
        'field'          => 'term_id',
        'terms'          => array(5)
    ))
));

// merging ids
$post_ids = array_merge( $first_ids, $second_ids);

// the main query
$query = new WP_Query(array(
    'post_type' => 'any',
    'post__in'  => $post_ids, 
    'orderby'   => 'date', 
    'order'     => 'DESC'
));

if( $query->have_posts() ):
    // here you go
endif;
5
  • Many thanks, personally it help me a lot to reduce size of a query with multiple relations (meta_query).
    – jjj
    Dec 28, 2018 at 4:39
  • Hi Sir, I followed your instruction but it displays Warning: array_merge(): Argument #1 is not an array Jan 4, 2019 at 14:58
  • You can add (array) before the get_posts() function to make sure the first_ids and second_ids values contains array. Ex: $first_ids = (array) get_posts(...
    – Shazzad
    Jan 5, 2019 at 15:37
  • But isn't this a bad practice? I mean the 3rd query should not be there. With just two requests you should already get all the data you need. If this was just PHP, you would just do these two queries, merge the data and loop it through. Mar 25, 2020 at 14:31
  • 1
    @user1203497 yes, third query is not actually required. merging results of those 2 queries and using ua_sort to order by dates will product the same outcome.
    – Shazzad
    Mar 26, 2020 at 15:06
0

Just one notification... this query is work for me also.. but if you use sticky post.. do not forget to add 'ignore_sticky_posts' => 1, and also you forget to add while - endwhile ... this was issues with my query ... so the "the main query" will be

// the main query
$query = new WP_Query(array(
    'post_type' => 'any',
    'post__in'  => $post_ids, 
    'orderby'   => 'date', 
    'order'     => 'DESC',
    'ignore_sticky_posts' => 1
));

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
echo the_title().'<br>';
endwhile; endif;

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.