Posted on

Ultimate FAQs

Ultimate FAQs is a FAQ plugin that can create FAQs. It has a search, and enabling Relevanssi may break that search.

There are two ways to fix these problems.

Using Relevanssi

The search comes up blank because Relevanssi takes over (it’s a regular search, after all), and the ufaq post type is not indexed. Index the post type, and the search should work.

Indexing the post type causes the ufaq posts to appear in the main site search. We need to modify the search queries to exclude the ufaq post type unless it’s explicitly required.

Here’s how:

add_filter( 'relevanssi_modify_wp_query', 'rlv_no_ufaqs' );
/**
 * Excludes the Ultimate FAQs FAQ post type from the search.
 *
 * Unless the 'post_type' query variable is set to the FAQ post type, this
 * adds the FAQ post type as an excluded post type to the 'post_type'.
 *
 * @param WP_Query $query The WP_Query object.
 */
function rlv_no_ufaqs( $query ) {
	if ( EWD_UFAQ_FAQ_POST_TYPE === $query->query_vars['post_type'] ) {
		return $query;
	}
    if ( ! empty( $query->query_vars['post_type'] ) ) {
		$query->query_vars['post_type'] .= ',';
	}
    $query->query_vars['post_type'] .= '-' . EWD_UFAQ_FAQ_POST_TYPE;
	return $query;
}

Add this to your site. This function will add the FAQ post type as a negative post type unless the search is explicitly asking for the FAQ post type.

Not using Relevanssi

Another option is to block Relevanssi out of the FAQ queries. Here’s how you do that:

add_filter( 'relevanssi_prevent_default_request', 'rlv_ufaq_query', 10, 2 );
add_filter( 'relevanssi_search_ok', 'rlv_ufaq_query', 10, 2 );
/**
 * Makes Relevanssi leave Ultimate FAQ searches alone.
 *
 * On relevanssi_prevent_default_request this function makes Relevanssi not block
 * the default WP query when the post_type parameter is set to the Ultimate FAQs
 * FAQ post type. On relevanssi_search_ok this function will make Relevanssi not
 * run the search.
 *
 * @param bool     $do_stuff Should Relevanssi functions do the things they do?
 * @param WP_Query $query    The WP_Query object.
 */
function rlv_ufaq_query( $do_stuff, $query ) {
	if ( EWD_UFAQ_FAQ_POST_TYPE === $query->query_vars['post_type'] ) {
		$do_stuff = false;
	}
	return $do_stuff;
}

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 *