Skip to main contentSkip to footer

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

ThemeCo
ThemeCo themes use custom codes for dynamic content. Those are not usual shortcodes, and Relevanssi won’t expand them automatically. In…
Indexing usermeta fields in Relevanssi

Relevanssi Premium can index user profiles. Users may have meta fields attached to them: there’s a wp_usermeta database table, even though the user editing interface by default doesn’t have any tools to add meta fields to users. Codex includes documentation on how to use the user meta fields. Read Working……with User Metadata for code examples. I’m sure there are also plugins that can help with this. Relevanssi settings page doesn’t contain a setting for indexing user meta fields. There’s, however, a hidden option you can use to index user custom fields. You can either set the option or use…

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