我最终选择了pre_get_posts
.
add_action(\'pre_get_posts\', \'custom__pre_get_posts\');
function custom__pre_get_posts($query){
//Get access to global variables
global $blog_id;
global $wp_query;
//Make sure we\'re not in admin, only run on the main query and don\'t run on our primary site
if($query->is_admin || !$query->is_main_query() || $blog_id===1){
return;
}
//Try running the query
$test_obj = $query->get_queried_object();
//If we didn\'t get a result then the page wasn\'t found
if(!$test_obj){
//Switch to the main site
switch_to_blog(1);
//Reset the query using the same args as the original (the previous reset changes $wpdb)
$wp_query = new WP_Query($query->query_vars);
//Re-query the object again. We might need to perform more sanity checks here
$test_obj = $query->get_queried_object();
//Switch back to our current site
restore_current_blog();
//
}else{
//This is a normal query for a page that exists, we don\'t need to do anything
}
}