Posted on

Martfury

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 child theme functions.php:

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();
}

Leave a Reply

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.