Getting Relevanssi Search Working in WordPress Multisite

We shifted the DLINQ site to multisite a while back. It’s no obvious change for anyone viewing but it enables us to spin up different sites without the hassle of jumping between sites or installing the same plugins/themes in so many places. Clearly not rocket science but little things like that build up over time, even with password managers.Or maybe I just missed multisite? Now I can make themes and plugin combinations that more specifical address needs we have. I’ve built out a documentation theme that I need to blog about and made another theme for a fellowship we have going. There’s also an intern handbook on there that’s expanding pretty rapidly.

One challenge that using a Multisite in this way brings up is searching across sites. We’re addressing that issue with Relevanssi Premium.I say in with a bad Italian accent and an extra “i” in honor of Jim Groom. It is a paid plugin but it deals with a couple of things that would be a hassle otherwise.

The major advantages I saw were.

  • searching across sites in multisite
  • indexing ACF fields
  • having a permanent, unlimited licensing option

Permanent, unlimited licensing is a nice option that is rare these days. I like to be able to use the same plugin to solve the same problem. It saves time and energy. Having unlimited usage keeps me from having to have a funding conversations with each individual project. That’s another chunk of overhead that builds up and one I always avoid if possible.

Making it Work

The documentation for Multisite search is decent. What I needed to do was add a switch_blog function into the search template so that the URLs to posts on other sites on the multisite would work.

The first option was to alter the theme’s search template. I ran into a bit of a problem though because the main DLINQ theme is using Avada.The theme to end all themes could be interpreted different ways. Avada’s search template is . . . expansive. I’m sure it makes good sense to those familiar with the theme but for me it was frustrating. Visual themes aren’t built to be modified on the backend.

I moved on to the next option, a plugin function made for visual themes. It felt a bit weird given the final bit was a shortcode to restore the original blog. I imagine there’s some place to put this in Avada but I didn’t find it immediately and I was irritated. I decided I could probably look for any search page and use the switch_to_blog function manually in combination with a content filter. That’s what you see below.


add_filter( 'the_content', 'rlv_restore_the_blog', 1 );
 
function rlv_restore_the_blog( $content ) {
     if ( is_search() ) {
        switch_to_blog(1);//only using it on the main site so this is hardcoded
        return $content;
    } else {
        return $content;
    }
}

Another Tweak

One of the major things we wanted the search to do was to index our new documentation site. That site uses a custom post type, article, to hold most of the content. Relevanssi does warn you that there are limits to Multisite searching.

  • There’s limited access to search parameters. You can use post_type, operator, meta_query, include_attachments and orderby.
  • Taxonomy terms from subsites can cause problems. Searching for taxonomy terms in taxonomies that exist in the current site works: for example searching for categories from different sites works, because every site has categories. However, if another subsite has a taxonomy that doesn’t exist in the current site, it cannot be found in the search.

The problem is that on the main site, there are indexing options and they didn’t include the article post type. That’s reasonable because that post type doesn’t exist on the main site but impacts how the other site gets indexed and makes it so I didn’t have a GUI way to weight aspects of that search. I just decided I’d add that custom post type to the main site BUT I would hide all the menu items for it so it wouldn’t confuse anyone. You can see the parts I commented with //HIDE ME below to hide them from the dashboard view but now I have the option to deal with this post type in Relevanssi. I haven’t tried it yet but I think I can do the same thing with taxonomies.

//article custom post type

// Register Custom Post Type article
// Post Type Key: article

function create_article_cpt() {

  $labels = array(
    'name' => __( 'Articles', 'Post Type General Name', 'textdomain' ),
    'singular_name' => __( 'Article', 'Post Type Singular Name', 'textdomain' ),
    'menu_name' => __( 'Article', 'textdomain' ),
    'name_admin_bar' => __( 'Article', 'textdomain' ),
    'archives' => __( 'Article Archives', 'textdomain' ),
    'attributes' => __( 'Article Attributes', 'textdomain' ),
    'parent_item_colon' => __( 'Article:', 'textdomain' ),
    'all_items' => __( 'All Articles', 'textdomain' ),
    'add_new_item' => __( 'Add New Article', 'textdomain' ),
    'add_new' => __( 'Add New', 'textdomain' ),
    'new_item' => __( 'New Article', 'textdomain' ),
    'edit_item' => __( 'Edit Article', 'textdomain' ),
    'update_item' => __( 'Update Article', 'textdomain' ),
    'view_item' => __( 'View Article', 'textdomain' ),
    'view_items' => __( 'View Articles', 'textdomain' ),
    'search_items' => __( 'Search Articles', 'textdomain' ),
    'not_found' => __( 'Not found', 'textdomain' ),
    'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
    'featured_image' => __( 'Featured Image', 'textdomain' ),
    'set_featured_image' => __( 'Set featured image', 'textdomain' ),
    'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
    'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
    'insert_into_item' => __( 'Insert into article', 'textdomain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this article', 'textdomain' ),
    'items_list' => __( 'Article list', 'textdomain' ),
    'items_list_navigation' => __( 'Article list navigation', 'textdomain' ),
    'filter_items_list' => __( 'Filter Article list', 'textdomain' ),
  );
  $args = array(
    'label' => __( 'article', 'textdomain' ),
    'description' => __( '', 'textdomain' ),
    'labels' => $labels,
    'menu_icon' => '',
    'supports' => array('title', 'editor', 'revisions', 'author', 'trackbacks', 'custom-fields', 'thumbnail',),
    'taxonomies' => array('category', 'post_tag'),
    'public' => true,
    'show_ui' => false,//HIDE ME
    'show_in_menu' => false,//HIDE ME
    'menu_position' => 5,
    'show_in_admin_bar' => false,//HIDE ME
    'show_in_nav_menus' => false,//HIDE ME
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => false,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
    'menu_icon' => 'dashicons-universal-access-alt',
  );
  register_post_type( 'article', $args );
  
  // flush rewrite rules because we changed the permalink structure
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
}
add_action( 'init', 'create_article_cpt', 0 );

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL