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

Download Monitor: Index PDF content for parent page

Relevanssi can handle Download Monitor files without significant problems. If you want to use the “Index PDF contents for the parent page” option, there’s a problem, though. Download Monitor comes in between: the PDF post is attached to the Download Monitor dlm_download post and not the page where the [download]……grab that. $download_id = preg_replace( ‘/[^\d]/’, ”, $meta_field_value ); if ( $download_id ) { // Replace the original post_parent ID with the new one. $query = preg_replace( ‘/post_parent = \d+/’, “post_parent = $download_id”, $query ); } } return $query; } If the [download] shortcode is somewhere in the post content,……string The modified MySQL query. */ function rlv_download_monitor( $query, $post_id ) { $post_object = relevanssi_get_post( $post_id ); $id_matches = preg_match_all( ‘/\[download id=.(\d+).\]/ims’, $post_object->post_content, $matches ); if ( $id_matches ) { $download_id = implode( ‘,’, $matches[1] ); $query = preg_replace( ‘/post_parent = \d+/’, “post_parent IN ($download_id)”, $query ); } return $query;…

EmbedPress autoembeds

EmbedPress has an auto-embed feature that tries to embed all URLs found in posts. This can cause problems with Relevanssi: searches can take a very long time when EmbedPress tries to fetch all outside URLs. The solution is simple: you can disable this feature during Relevanssi excerpt creation. Add this…

Adding direct links to outside pages to search results

…singular name’, ‘textdomain’ ), ‘menu_name’ => _x( ‘Affiliate links’, ‘Admin Menu text‘, ‘textdomain’ ), ‘name_admin_bar’ => _x( ‘Affiliate link’, ‘Add New on Toolbar’, ‘textdomain’ ), ‘add_new’ => __( ‘Add New’, ‘textdomain’ ), ‘add_new_item’ => __( ‘Add New Affiliate link’, ‘textdomain’ ), ‘new_item’ => __( ‘New Affiliate link’, ‘textdomain’ ), ‘edit_item’……=> __( ‘Edit Affiliate link’, ‘textdomain’ ), ‘view_item’ => __( ‘View Affiliate link’, ‘textdomain’ ), ‘all_items’ => __( ‘All Affiliate links’, ‘textdomain’ ), ‘search_items’ => __( ‘Search Affiliate links’, ‘textdomain’ ), ‘parent_item_colon’ => __( ‘Parent Affiliate links:’, ‘textdomain’ ), ‘not_found’ => __( ‘No Affiliate links found.’, ‘textdomain’ ), ‘not_found_in_trash’ =>……__( ‘No Affiliate links found in Trash.’, ‘textdomain’ ), ‘archives’ => _x( ‘Affiliate link archives’, ‘The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4’, ‘textdomain’ ), ‘filter_items_list’ => _x( ‘Filter Affiliate links list’, ‘Screen reader text for the filter links heading on the post…

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