I am trying to retrieve search results from one multi-site site (“Staffsite” – id: 2
) and display them alongside the search results on another site that’s part of the multi-site setup (“Flagship” – id: 1
). I currently have the following code in search.php
on Flagship:
$args = array(
'post_type' => 'staff',
's' => get_search_query(),
);
switch_to_blog( 2 ); // switch to Staffsite
$staffsite_query = new WP_Query( $args );
var_dump( $staffsite_query->post_count ); // 0
// Do something with post data
restore_current_blog(); // return to Flagship
The query works as expected when used in a template on Staffsite (replacing get_search_query()
with an actual value), returning all the relevant posts, but does not work when used from a template on Flagship in combination with switch_to_blog()
.
For what reason does this not work?