Posted on

Ultimate FAQs

Ultimate FAQs is a FAQ plugin that can be used to create FAQs and add them on your site. The plugin has a search that searches the FAQs on the FAQ page, and if Relevanssi is enabled, that search may be broken. There are two ways to fix these problems.

Using Relevanssi

The reason the search comes up blank is 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.

This then causes the ufaq posts to appear in the main site search. That’s not desired. We need to modify the search queries so that the ufaq post type is excluded from the search, 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 theme functions.php. This function will add the FAQ post type as a negative post type, unless the search is specifically asking for the FAQ post type.

Not using Relevanssi

Another option is to simply 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 Releavnssi 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;
}

Add this to your theme functions.php.

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.