Relevanssi: How to Exclude WooCommerce Product Variations From WordPress Search Results

By default, Relevanssi doesn’t un-include search results that are draft, pending, private, etc. This is problematic since you may not want products to be available via search.

This solution takes a product variation’s parent post status into account. If the variation’s parent in question is anything but ‘publish’ it de-indexes the product variation.

Add it to your functions.php and rebuild your index.

add_filter( 'relevanssi_do_not_index', 'relevanssi_search_do_not_index', 10, 2 );

function relevanssi_search_do_not_index( $exclude, $post_id ) {

	// get post
	$post = get_post( $post_id );

	// exclude if post is variation and it's parent post status is not publish OR posts status is not publish
	if( ( $post->post_type == 'product_variation' && get_post_status( $post->post_parent )  != 'publish' ) || $post->post_status != 'publish' ) {

		$exclude = true;

	} 

	return $exclude;

}
Posted in: Code Samples, Development  |  Tagged with: , , ,  |  Leave a comment

Leave a Reply