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

Can Relevanssi access content outside WordPress?

Short answer: no. Relevanssi can only access WordPress content. For a longer answer, read on. Only within single WP installation Relevanssi only operates within one WordPress installation. Relevanssi Premium supports multisite searching, where the search can return posts from more than one subsite at once, but even in those cases……the WordPress sites must be part of the same installation. It’s not possible to search content that exists in another WordPress installation, not even if it’s on the same server. No outside content Relevanssi is heavily dependent on WordPress methods. When getting posts, Relevanssi uses the get_post() function in WordPress……a custom post type, import the data and have Relevanssi index the post type, and then in the search results change the links on the fly to point to the actual source and not the custom post type post. For small amounts of data, this may be a feasible approach,…

Adjusting search throttle
If Relevanssi Premium causes out-of-memory problems, make sure you have the “Throttle searches” option checked. However, if that doesn’t work…

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