Skip to main contentSkip to footer

It’s possible to integrate all kinds of external data to Relevanssi weights. Post Views Counter is an analytics plugin that collects stats about your visitors and stores them in the local database, which means those stats are available for Relevanssi.

For some sites, this makes a lot of sense. For example, I have Kirjavinkit, a book review site with an archive of about 10,000 book review posts. The popularity of the posts is primarily driven by external search traffic and seems like a good indicator of what users find interesting and valuable.

Integrating the Post Views Counter stats as a factor in the weight calculations is straightforward. The best tool to use is the relevanssi_results filter hook. It lets us modify the weights of the posts. For each post, we calculate a weight multiplier based on the visitor stats and then multiple the weight of the post with that.

I add some normalization here. I normalize all weights to the 1–1,000 range. I also set the minimum weight to 1 to avoid punishing the rarely-visited posts.

To avoid performance problems, I fetch all the stats once and store them in a global variable so that when the function processes all posts, it only needs to make one database request.

Here’s the code:

function rlv_pvc_weight( int $post_id ) : float {
	global $relevanssi_pvc_weights;

	if ( empty( $relevanssi_pvc_weights ) ) {
		global $wpdb;
		$stats = $wpdb->get_results( "SELECT id, count FROM {$wpdb->prefix}post_views WHERE type = 4" );
		foreach ( $stats as $post_stats ) {
			$relevanssi_pvc_weights[ $post_stats->id ] = $post_stats->count;
		}
		$max_visits = max( $relevanssi_pvc_weights );
		$factor     = $max_visits / 1000; // This controls the normalization.
		foreach ( $relevanssi_pvc_weights as $post_id => $visits ) {
			$weight = $visits / $factor;
			if ( $weight < 1 ) {
				$weight = 1;
			}

			$relevanssi_pvc_weights[ $post_id ] = $weight;
		}
	}
	return $relevanssi_pvc_weights[ $post_id ] ?? 1;
}

add_filter( 'relevanssi_results', 'rlv_pvc_weights' );

function rlv_pvc_weights( $post_weights ) {
	foreach ( $post_weights as $post_id => $weight ) {
		$post_weights[ $post_id ] = rlv_pvc_weight( intval( $post_id ) ) * $weight;
	}
	return $post_weights;
}

Your account

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

Search

Popular Resources

Partial matches to just one custom field

…partial matches but only in the custom field ‘sku‘. * * For posts that have partial matches outside the custom field ‘sku‘, * set the weight to to eliminate them from the search results. * * @param object $match The match object with the custom field detail. * @param int……) ) { $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…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…

WooCommerce: Aelia Prices by Country product visibility

…$post = get_post( $post_id ); $post_type = $post->post_type; $product_id = $post->ID; if ( ‘product‘ === $post_type && function_exists( ‘wc_get_product’ ) ) { // Get the product object. $product = wc_get_product( $product_id ); // Check if the product is purchasable. if ( $product && ! $product->is_purchasable() ) { $post_ok = false;……Check if the product is purchasable. if ( $product && ! $product->is_purchasable() ) { $post_ok = false; } } return $post_ok; } This function uses the relevanssi_post_ok filter hook to control which posts are included in the search and the $product->is_purchasable() function to see if the product can be shown….Aelia has a Prices by Country for WooCommerce plugin, which can adjust prices based on customer country and hide products unavailable in a specific country. Jason James shared a function that makes Relevanssi hide unavailable products in search: add_filter( ‘relevanssi_post_ok’, ‘relevanssi_aelia_compatibility’, 10, 2 ); function relevanssi_aelia_compatibility( $post_ok, $post_id ) {…

WordCamp Nordic 2019
Relevanssi Premium is a proud sponsor of the inaugural WordCamp Nordic. There have been several WordCamps in Nordic countries, but…

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