Posted on

FacetWP

Relevanssi is generally well compatible with FacetWP, which provides advanced filtering and faceting capabilities for the search. There are, however, some situations where Relevanssi and FacetWP do not work well.

Make sure you are using the Relevanssi integration plugin for FacetWP. It covers most of the problems.

Disabling the FacetWP integration

In some cases, however, it’s necessary to disable the FacetWP integration. This comes to play when you are not using FacetWP for Relevanssi searches. In that case, using the Relevanssi integration plugin will break the Relevanssi search, but not using it will break the FacetWP search facet.

For this, the solution is easy:

add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
	if ( $query->is_search() && $query->is_main_query() ) {
		$is_main_query = false;
	}

	return $is_main_query;
}, 5, 2 );

Add this snippet to your site (source) to disable the FacetWP Relevanssi integration; everything should work fine.

Searching for users and taxonomy terms

FacetWP can’t handle how Relevanssi integrates user profiles and taxonomy terms in the search results. Relevanssi gives them post ID -1, and FacetWP can’t deal with that. If you want to use FacetWP to search and search for users, use the FacetWP user searching method, where FacetWP generates a separate post type for users. That can be searched with Relevanssi.

FacetWP and WooCommerce themes

Using Relevanssi, WooCommerce themes (like Flatsome), and FacetWP Relevanssi integration 0.7.3 may cause problems where the product search results template doesn’t show up correctly. These problems happen because the theme checks the post_type parameter to determine when to show the product search results page. FacetWP Relevanssi integration 0.7.3 unsets the post_type parameter to use the Relevanssi-specific post_types parameter, but the theme doesn’t understand that.

The fix for this is to reset the post_type parameter. Add this to your site:

add_action( 'pre_get_posts', function( $query ) { 
  if ( 'product' === $query->get( 'post_types' ) ) { 
    $query->set( 'post_type', 'product' ); 
  } 
}, 1001 ); 

Using Relevanssi excerpts with FacetWP

FacetWP only asks Relevanssi to provide post IDs as results, which means excerpts are not included. Thus, when you display post excerpts in the FacetWP templates, Relevanssi-generated excerpts are not available.

Here’s a code snippet that can be used within FacetWP templates to generate and display Relevanssi excerpts for the posts:

$query = isset( FWP()->facet->facets['search'] )
	? FWP()->facet->facets['search']['selected_values']
    : '';
echo relevanssi_do_excerpt( $post, $query );

Using relevanssi_do_query filter functions with FacetWP

There may be occasions where you need to manipulate the Relevanssi query FacetWP runs using the relevanssi_modify_wp_query filter hook. Because the FacetWP Relevanssi integration add-on calls relevanssi_do_query() directly, this hook, and several others, are not called. None of the functions added to the relevanssi_modify_wp_query hook run in this case.

The solution is to use the facetwp_relevanssi_do_query hook from within the FacetWP Relevanssi integration plugin instead. This hook can be located in the run_query() function in the facetwp-relevanssi.php file provided by the plugin. It works similarly to relevanssi_modify_wp_query, allowing you to create a function that manipulates the Relevanssi query when the search is performed by FacetWP. The function has one parameter, which contains the WP_Query object used to perform the query.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *