Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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:

//setup your queries as you already do
$query1 = new WP_Query($args_for_query1);
$query2 = new WP_Query($args_for_query2);

//create new empty query and populate it with the other two
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );

//populate post_count count for the loop to work correctly
$wp_query->post_count = $query1->post_count + $query2->post_count;
share|improve this answer
    
This looks good, but I am looking for a solution that can be used with the pre_get_posts hook - i.e., I need to modify an existing WP_Query object. I have tried using '$wp_query->init();' instead of '$wp_query = new WP_Query();', but that seems to mess things up. Any suggestions? –  ban-geoengineering Dec 30 '13 at 22:14
1  
You should add a different question since it's a different answer, but you can use this same code but only the query2 parts, modifying the ->posts and ->post_count parts of the $wp_query object to have the sum of the original query that pre_get_posts gives you and the second query you want to append. –  guidod Jan 3 at 15:32

So, if you have this:

$number1 = new WP_Query($multisitequery);

$number2 = new WP_Query($normalquery);

I assume you define these somewhere previous?

$multisitequery = array();
$normalquery = array();

...in which case, to merge the two queries, just array_merge() the two arrays before passing them to new WP_Query():

$merged_query_args = array_merge( $normalquery, $multisitequery );

$merged_query = new WP_Query( $merged_query_args );

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.

share|improve this answer
    
Thank you for replying Chip, much appreciated. I have edited my original question to give you a better idea of what i have to work with. Many thanks indeed and i look forward to another knowledge packed reply lol Thanks –  Rory Rothon Jun 17 '12 at 22:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.