SureCart Shop Search with Relevanssi (Ticket 1546989)

This demo site runs SureCart with Relevanssi active, the same combination as your store. The four demo products are modelled on your shop, so the same search words work.

Try it

Why the shop search came back empty

The SureCart Product Search block does not use the site search. When you type, it reloads the product list with ?products-search=toolkit and SureCart runs its own WordPress query for matching products.

Relevanssi replaces every front-end WordPress query that contains a search term with an empty one (SELECT * FROM wp_posts WHERE 1=2) to save database work, because it plans to supply the results itself. It only supplies them for the main site search results page. The SureCart product list is a separate query, so it is emptied and nothing refills it. Relevanssi documents this on its relevanssi_prevent_default_request page, together with the switch used below.

The fix

This tells Relevanssi to leave the SureCart product list query alone. The regular site search stays with Relevanssi.

/**
 * SureCart shop search + Relevanssi.
 *
 * Relevanssi replaces every front-end WordPress query that contains a search
 * term with an empty one ("SELECT * FROM wp_posts WHERE 1=2") and only fills in
 * its own results for the main site search. The SureCart Product List (the
 * search box on the shop page) runs its own product query, so it always ended
 * up with "No products found."
 *
 * This tells Relevanssi to leave that one query alone, so WordPress runs the
 * product search itself. The normal site search stays with Relevanssi.
 */
function cpj_surecart_skip_relevanssi( $value, $query ) {
	$is_surecart_product_list = $query instanceof WP_Query
		&& ! $query->is_main_query()
		&& in_array( 'sc_product', (array) $query->get( 'post_type' ), true );

	return $is_surecart_product_list ? false : $value;
}
add_filter( 'relevanssi_prevent_default_request', 'cpj_surecart_skip_relevanssi', 10, 2 );
add_filter( 'relevanssi_search_ok', 'cpj_surecart_skip_relevanssi', 10, 2 );

How to add it (WPCode, free)

  1. Plugins > Add New, search for WPCode, install and activate it.
  2. Code Snippets > + Add Snippet > Add Your Custom Code (New Snippet) > PHP Snippet.
  3. Give it a title, paste the code above into the Code Preview box.
  4. Insertion: Auto Insert, Location: Run Everywhere.
  5. Switch it to Active and click Save Snippet.
  6. Clear your site cache, then type a product name in the shop search box.

Prefer a child theme? Paste the same code at the end of your child theme’s functions.php instead.

Scroll to Top