Skip to main contentSkip to footer

Partial matches to just one custom field

Is it possible to set (with filters and/or actions/functions) Relevanssi to use whole words keyword matching for post titles and 2 custom fields, and partial keyword matching only for a specific custom field (sku in this case)?

Indeed it is! That is, you can’t set different keyword matching methods to different parts of the post, but you can enable partial matching and then discard partial matches for the parts where they are not wanted.

To allow partial matches, but only in the custom field sku, you can do this with relevanssi_match:

add_filter( 'relevanssi_match', 'rlv_partial_partial', 10, 3 );

/**
 * Allows partial matches but only in the custom field 'sku'.
 *
 * For posts that have partial matches outside the custom field 'sku',
 * set the weight to 0 to eliminate them from the search results.
 *
 * @param object $match The match object with the custom field detail.
 * @param int    $idf   The IDF value for calculating weight, not used.
 * @param string $term  The search term.
 *
 * @return object $match The modified match object.
 */
function rlv_partial_partial( $match, $idf, $term ) {
	if ( $term !== $match->term ) {
		$cf_detail = json_decode( $match->customfield_detail );
		if ( ! isset( $cf_detail->sku ) ) {
			$match->weight = 0;
		}
	}
	return $match;
}

This requires Relevanssi Premium, because in the free version the customfield_detail field is not included in the $match object. (It’s possible to replicate the functionality by checking the sku custom field with get_post_meta( $match->doc, 'sku', true ); but that adds a lot of database queries and makes the search perform worse).

Your account

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

Search

Popular Resources

Most important Relevanssi debugging trick

…results page, usually search.php. See if there’s a call to query_posts() and if there is, comment it out. In most cases that is enough to fix the problem. It’s not always query_posts(). Appearance of new WP_Query() is another sign of trouble, or $wp_query->get_posts();. Anything that queries the database for new…

Why is a post type excluded from search?

…is registered has not changed: it’s still the same. Relevanssi just doesn’t care about it and still indexes the post. The setting does affect Relevanssi in that when Relevanssi does search posts, Relevanssi does respect this setting and will exclude these posts from the searches. That’s often for the best;…In the list of post types to index in the Relevanssi indexing settings tab there’s a column “Excluded from search”, which has a value of “yes” or “no” for each post type. What does this mean, and why does the value remain “yes” even if you set Relevanssi to index……that post type? This column is strictly for information only, and does not govern what Relevanssi does. This is something that is defined when a post type is registered. Some post types are meant for public use (like post or page) and thus are not excluded from the search. Other…

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