Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combine multiple WordPress queries
<?php
// An example of creating two separate WP queries, combining the results,
// sorting by date and formatting the results for us in a loop like a regular query.
// order the posts by date in descending order (should go in functions.php to keep things tidy)
function order_by_date( $a, $b )
{
return strcmp( $b->post_date, $a->post_date );
}
// get the posts for the first query
$q1_args = array(
// args for the first query
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
// args for the second query
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
setup_postdata( $post );
// Now we can use template tags as if this was in a normal WP loop
the_title('<h2>','</h2>');
the_content();
}
?>
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 16, 2014

You may want to wrap array_merge with array_unique, to ensure no duplicate posts exists. Array_merge will not remove duplicates unless they have the same numeric key. See WP.org forum post: http://wordpress.org/support/topic/multiple-queries-compiling-into-one-loop?replies=5#post-1920638

$final_posts = array_unique( array_merge( $q1_posts, $q2_posts ) );
@Alligatore3

This comment has been minimized.

Copy link

@Alligatore3 Alligatore3 commented Nov 25, 2014

This is an extract of my code...Have a look

    <?php
$args = array( 'post_type' => 'post' , 'posts_per_page' => 6 );
$wp_query = new WP_Query( $args );  

$sgarbi = array(
    'post_type' => 'post_Attivita' ,
    'meta_query' => array(
            array(
                'numberposts' => -1,
                'post_type' => 'post_Attivita',
                'key' => 'pubblica_in_home', // name of custom field
                'value' => '1', // matches exaclty "red", not just red. This prevents a match for "acquired"
                'compare' => '=='
            )
        )
    );
$wp_query_sgarbi = new WP_Query( $sgarbi );

$result = new WP_Query();

// start putting the contents in the new object
$result->posts = array_merge( $wp_query->posts, $wp_query_sgarbi->posts );

// here you might wanna apply some sort of sorting on $result->posts

// we also need to set post count correctly so as to enable the looping
$result->post_count = count( $result->posts );

for($i = 1; $result->have_posts(); $i++) {                          
    $result->the_post();
@EastSideCode

This comment has been minimized.

Copy link

@EastSideCode EastSideCode commented Sep 23, 2018

You may want to wrap array_merge with array_unique, to ensure no duplicate posts exists. Array_merge will not remove duplicates unless they have the same numeric key. See WP.org forum post: http://wordpress.org/support/topic/multiple-queries-compiling-into-one-loop?replies=5#post-1920638

$final_posts = array_unique( array_merge( $q1_posts, $q2_posts ) );

This won't work without the SORT_REGULAR flag.

array_unique( array_merge( $q1_posts, $q2_posts ), SORT_REGULAR );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment