I am running a multisite network and i have setup a sql query that uses swith_to_blog(); and queries the posts.
Is there a way that i can declare the query inside a new WP_Query and actually merge that query with another?
Basically if i do this:
$number1 = new WP_Query($multisitequery);
Can i merge it with:
$number2 = new WP_Query($normalquery);
$normalquery
holds settings like pagination, per page, excerpt, title etc… on a portfolio shortcode.
I would like it to include queried posts from my new $multisite
query.
Can this be achieved? Just wanting to save me from creating a whole new shortcode setup lol
Many thanks in advance.
Rory
EDIT========
What i have is:
$portfolio = array();
$portfolio = $settings;
Further down my portfolio function “after all the $settings[‘options’]” i have:
$portfolio_query = new WP_Query( $portfolio );
the $portfolio_query
uses a loop on a page template.
I want to add an extra query into this like so:
global $wpdb, $blog_id, $post;
$blogs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_blogs ORDER BY blog_id" ) );
$globalcontainer = array();
foreach ($blogs as $blog){
switch_to_blog($blog->blog_id);
$globalquery = query_posts($args);
$globalcontainer = array_merge( $globalcontainer, $globalquery );
restore_current_blog();
}
where i assume that $globalcontainer
would be the array to merge into the wp_query();
.
So taking onboard what you have replied with, in theory i could simply:
$mergedqueryargs = array_merge($portfolio , $globalcontainer);
$portfolio_query = new WP_query($mergedqueryargs);
Would that be correct?
Second, regarding the array_merge array key overwrite…..
How would i go about stopping an overwrite?
You won’t do much good just merging the arguments, you need to merge the resulting posts array and the post_count count. This works for me:
I generally merge their ID arrays and make a third query. To keep first set of queries cheap I only return their ID’s using fields parameter like this:
Hope this helps
—EDIT—
After my answer original question is edited for multisite details. In case of multisite post merges this does not work.
So, if you have this:
I assume you define these somewhere previous?
…in which case, to merge the two queries, just
array_merge()
the two arrays before passing them tonew WP_Query()
:Note that order is important in the
array_merge()
call. If both have the same array key, the second array will overwrite the first array.Thus, you can glue two WP_Query queries with different post_type or other sorting data, for example, and then glue them by ids into one WP_Query query that will fully work – in order to preserve the sorting when gluing, you need to specify orderby => post__in
my example is how it creates a new WP_Query cycle and can affect any other cycle for displaying posts by calling new global_change_of_sorting_posts->change_query_posts() => query_posts() you need to call before processing the cycle for displaying posts!