Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevenspads/1392ac08ccf004af50c8c3a325fbff0d to your computer and use it in GitHub Desktop.
Save stevenspads/1392ac08ccf004af50c8c3a325fbff0d to your computer and use it in GitHub Desktop.
Merging WP_QUERY objects in WordPress
//Your original 2 WP_Query results:
$query1 = new WP_Query($args_for_query1);
$query2 = new WP_Query($args_for_query2);
//Create a new WP_Query object to hold the joining of the 2 originals:
$joined_query = new WP_Query();
$joined_query->posts = array_merge( $query1->posts, $query2->posts );
//Update joined object's post count:
$joined_query->post_count = $query1->post_count + $query2->post_count;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment