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?
switch_to_network()
that's the equivalent ofswitch_to_blog()
. You might find what you need in the WP Multi Network plugin.staff
post type defined in a plugin? If so, and the plugin is only activated on blog 2, then its code won't be loaded on a request for blog 1, even when you useswitch_to_blog
, so it won't recognize the post type in the query.