There’s a compatibility issue between Relevanssi and Gutenberg Full Site Editing. No results are found when you create a Query Loop to display the search results with Relevanssi enabled. The blank results happen because the post template block doesn’t use the Relevanssi results but instead uses a new WP_Query. Since Relevanssi still blocks the default search, this comes up empty.
For a straightforward solution, add this to your site:
add_filter( 'relevanssi_search_ok', function( $ok, $query ) { if ( ! empty( $query->query_vars['s'] ) ) { $ok = true; } return $ok; }, 10, 2 );
This function hooks to the relevanssi_search_ok
filter hook. This hook fires because Relevanssi looks at the newly-created WP_Query, but since it’s not the main query for the template, Relevanssi doesn’t take over. This function instructs Relevanssi to take over as long as there’s a s
parameter in the query, and a search term exists.
Excerpts
The core WP excerpt block does not allow Relevanssi highlights in excerpts. It will display Relevanssi excerpts (restricted to the excerpt length in the block settings – make sure the setting in the block is higher than the Relevanssi excerpt length setting), but no highlights will be visible because all the excerpts are passed through wp_trim_words()
which removes HTML tags.
This function can be used to overcome this limitation:
add_filter( 'wp_trim_words', function( $text, $num_words, $more, $original_text ) { global $post; if ( isset( $post->relevance_score ) ) { return $post->post_excerpt; } return $text; }, 10, 4 );
If the post has the relevance_score
attribute set – which should only happen within a search context – this function will cause the wp_trim_words()
to return the original post excerpt.
With Max Mega Menu, the function above will add the excerpts to the menu items. You can avoid that:
add_filter( 'wp_trim_words', function( $text, $num_words, $more, $original_text ) { global $post; if ( ! empty( $text ) && isset( $post->relevance_score ) ) { return $post->post_excerpt; } return $text; }, 10, 4 );
Restrictions
Relevanssi cannot add the highlight
parameter to the permalinks in FSE themes. This means you cannot use the “Highlight in documents” feature.
Thank you for the clarification!
Quick question: Is this problem something that should be fixed in Twenty Twenty-Two itself or is this underlying issue only applicable to Relevanssi and is it something caused by the plugin that you plan to fix in future releases?
I am asking because, if this is coming from the theme, we should open an issue in trac at https://core.trac.wordpress.org/ (I am more than happy to open it myself, I just want to make sure I understand the details first).
Thanks again, your plugin rocks!
The underlying issue is the way the Query Loop block in Gutenberg works, so this applies to all themes with full site editing, not just Twenty Twenty-Two. I don’t think opening a ticket will help; it’s quite likely this needs to be done this way in the Query Loop. Relevanssi will likely automatically use this filter function for sites that use full site editing in the future, so this should not be a major problem.