Skip to main contentSkip to footer

It’s possible to integrate all kinds of external data to Relevanssi weights. Koko Analytics is a great analytics plugin. It 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 Koko 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_koko_weight( int $post_id ) : float {
	global $relevanssi_koko_weights;

	if ( empty( $relevanssi_koko_weights ) ) {
		global $wpdb;
		$stats = $wpdb->get_results( "SELECT id, SUM(visitors) AS visits FROM {$wpdb->prefix}koko_analytics_post_stats GROUP BY id" );
		foreach ( $stats as $post_stats ) {
			$relevanssi_koko_weights[ $post_stats->id ] = $post_stats->visits;
		}
		$max_visits = max( $relevanssi_koko_weights );
		$factor     = $max_visits / 1000; // This controls the normalization.
		foreach ( $relevanssi_koko_weights as $post_id => $visits ) {
			$weight = $visits / $factor;
			if ( $weight < 1 ) {
				$weight = 1;
			}

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

add_filter( 'relevanssi_results', 'rlv_koko_weights' );

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

Your account

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

Search

Popular Resources

Moving the admin search page
Jules Colle came up with a method of moving the Relevanssi admin search page from under the Dashboard heading to…
Menu problems in multisite search

Sometimes multisite searches cause problems in navigation menus. This is caused by pages from another subsite going into the page cache, and then replacing a page in the navigation menu with the same page ID. If you’re having this problem, adding the following code to the theme functions.php should solve…

MemberPress Downloads add-on
The Downloads add-on for MemberPress adds downloadable files to MemberPress. These files are stored outside the Media Library, so by…

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