WP_Query_Multisite icon indicating copy to clipboard operation
WP_Query_Multisite copied to clipboard

How to default to multisite?

Open iversoncreative opened this issue 4 years ago • 1 comments

How would one default all queries to use multisite?

iversoncreative avatar Aug 24 '21 23:08 iversoncreative

Well, this is a year and some change late, but this may help.

It hooks into 'query_vars' and adds the query vars that enable multisite searches as the default, but only when on the front-end of the site, and only if it's actually the main search query.

'sites__in' and 'sites__not_in' have been commented out, but if you know specific site IDs that you only want to search, or specific site IDs that you absolutely don't want to search then you could uncomment them and fill in the IDs in the array.

THIS IS UNTESTED!!!! (but will probably work as-is, assuming you've properly included/required the WP_Query_Multisite class file):

add_filter('query_vars', 'permanent_ms_search', 10, 1);
function permanent_ms_search($vars) {
    global $wp_query;
    if( !is_admin() && !is_network_admin() && $wp_query->is_main_query() && $wp_query->is_search ) {
        $vars['multisite'] = 1; // enable multisite search
        //$vars['sites__in'] = array(); // array of specific site IDs to search
        //$vars['sites__not_in'] = array(); // array of specific site IDs to exclude from searches
    }
    return $vars;
}

I'm actually writing a full-blown plugin to handle multisite searches that uses a modified version of this WP_Query_Multisite class. It has Admin options built-in to handle exactly what you're asking for. It sets up network-wide defaults, and allows for per-site overrides at the network level and optionally within sub-site admin panels. I'll be posting it on GitHub and the WP plugin directory. Keep an eye out for it in the near future. Teaser Picture

codejp3 avatar Nov 15 '22 04:11 codejp3