Skip to main contentSkip to footer

Martfury is a WooCommerce theme that comes with a built-in dropdown search. That’s great, except that as usual the dropdown search doesn’t work with Relevanssi. Fortunately the search is hooked into place, so it’s possible to hook it out and replace it with something new.

That might not be necessary, though. The theme developer has said they will add a filter hook to adjust the query arguments for the get_posts() call the search is doing. If they do, then fixing this in most cases is as simple as adding the relevanssi parameter to the query like this:

add_filter( 'martfury_filter_hook_name', 'rlv_use_relevanssi' );
function rlv_use_relevanssi( $args ) {
  $args['relevanssi'] = true;
  return $args;
}

Replace the filter hook name with whatever it is. I don’t know yet, and I’m not sure if this is how they’ll end up implementing it; hopefully they do.

However, this does not work in every case. If you want to include user profiles and taxonomy terms in the search, and not products and other posts, those non-post items won’t appear correctly in the search. The permalinks will be wrong. That’s something you can’t fix with a simple filter hook, and instead you need to replace the whole search.

Add this to your site:

add_action( 'wp_loaded', 'rlv_replace_martfury_search' );
/**
 * Unhooks the Martfury search and replaces it with our custom search.
 */
function rlv_replace_martfury_search() {
	global $wp_filter;
	unset( $wp_filter['wc_ajax_martfury_search_products']->callbacks[10] );
	add_filter( 'wc_ajax_martfury_search_products', 'rlv_instance_search_result' );
}

/**
 * Custom replacement for the dropdown search. Includes all post types in the
 * search.
 */
function rlv_instance_search_result() {
	if ( apply_filters( 'martfury_check_ajax_referer', true ) ) {
		check_ajax_referer( '_martfury_nonce', 'nonce' );
	}
  
	$response      = array();
	$result_number = intval( martfury_get_option( 'header_ajax_search_results_number' ) );
	$args          = array(
		'post_type'        => 'any',
		'posts_per_page'   => $result_number,
		's'                => trim( $_POST['term'] ),
		'suppress_filters' => 0,
		'relevanssi'       => true,
	);
	// If necessary, you can adjust the search parameters here.

	$posts    = get_posts( $args );
	$post_ids = array();
	foreach ( $posts as $post ) {
		$id = $post->ID;

		if ( ! in_array( $id, $post_ids ) ) {
			$post_ids[] = $id;
			$post_link  = $post->relevanssi_link ?? get_permalink( $id );
			$response[] = sprintf(
				'<li>' .
				'<a class="image-item" href="%s">' .
				'%s' .
				'</a>' .
				'<div class="content-item">' .
				'<a class="title-item" href="%s">' .
				'%s' .
				'</a>' .
				'</li>',
				esc_url( $post_link ),
				get_the_post_thumbnail( $id ),
				esc_url( $post_link ),
				$post->post_title
			); // You can adjust how the search results look like here.
		}
	}

	if ( empty( $response ) ) {
		$response[] = sprintf( '<li>%s</li>', esc_html__( 'Nothing found', 'martfury' ) );
	}

	$output = sprintf( '<ul>%s</ul>', implode( ' ', $response ) );

	wp_send_json_success( $output );
	die();
}

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

German umlauts

By default, the search ignores German umlauts. Searching for “uber” will also find “über” and so on. This ignorance is not a Relevanssi feature but instead governed by your database. The default database collation WordPress uses (utf8mb4_unicode_ci) ignores all accents, including umlauts. If you want the search to care about……umlauts, you can change the collation. For German, use utf8mb4_german2_ci (actually, use utf8mb4_swedish_ci, see comments below): Now, the database will not ignore umlauts. Relevanssi also removes accents to match what the database does, so you must also undo that. Add this to your site: remove_filter( ‘relevanssi_remove_punctuation’, ‘remove_accents’, 9 ); Once…

A New Chapter for Relevanssi
We are very excited to announce that Relevanssi has found a new home with comesio solutions GmbH, a software company…
Divi

Make Divi include all post types Divi search form module restricts the search to only posts and pages. To get rid of that restriction, you can add this to your site: add_action( ‘pre_get_posts’, function() { unset( $_GET[‘et_pb_searchform_submit’] ); }, 1 ); Divi and multisite searching Divi search results templates don’t……work in multisite searching. Multisite searching requires changes to the templates, and it’s impossible to make those changes in the Divi Blog module. If you want to do multisite searching with Divi, you need to use a standard WordPress search results template for the results. Getting Divi to show Relevanssi……excerpts Divi doesn’t use the default excerpts but instead truncates the beginning of the post and shows the first 270 characters. That means Relevanssi-generated excerpts don’t show up at all. However, you can configure Divi to show Relevanssi excerpts. Just enable the “Use excerpts when defined” setting in Divi settings,…

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed