relevanssi_indexing_restriction

apply_filters( 'relevanssi_indexing_restriction', array $restriction )

Adds restrictions to the indexing MySQL query to exclude posts from the index.

Parameters

$restriction
(array) An array with two keys: mysql has the MySQL code and reason has an explanatory text for the restriction.

More information

As described in the relevanssi_indexing_query documentation, Relevanssi uses a MySQL query to fetch the indexed post IDs. If you want to exclude posts from being indexed, it’s best to manipulate this query. (It’s also possible to use relevanssi_do_not_index to exclude posts, but if you’re excluding more than a couple of posts, it’s better to use relevanssi_indexing_restriction if possible.)

This filter hook can be used to add new restrictions to the query. The query looks like this:

SELECT post.ID
  FROM wp_posts post
  LEFT JOIN wp_posts parent ON (post.post_parent=parent.ID)
  WHERE
    (post.post_status IN ('publish','draft','private')
      OR
      (post.post_status='inherit' AND(
        (parent.ID is not null AND (parent.post_status IN ('publish','draft','private')))
        OR (post.post_parent=0)
      ))
    )
  ORDER BY post.ID DESC

The new restrictions from this filter hook are added to the WHERE clause in this query, so the restrictions need to be valid MySQL. For example, this is how the All-in-One SEO indexing restriction is done:

function relevanssi_aioseo_exclude( array $restriction ) {
	if ( 'on' !== get_option( 'relevanssi_seo_noindex' ) ) {
		return $restriction;
	}

	global $wpdb;

	$restriction['mysql']  .= " AND post.ID NOT IN (SELECT post_id FROM
		{$wpdb->prefix}aioseo_posts WHERE robots_noindex = '1' ) ";
	$restriction['reason'] .= ' All-in-One SEO';

	return $restriction;
}

If the option is enabled, this will add a restriction that the indexed posts must not have the robots_noindex value set to 1 in the wp_aioseo_posts database table.

Note the .= in the assignment. When adding new restrictions, note that there may be existing restrictions from earlier filter functions (and more may be added later). Thus you need to make sure you append your additions instead of just assigning them.

The reason value is just for information and will appear, for example, in the Relevanssi debugger. It’s not mandatory, but putting something informative there will help you a lot when trying to figure out why a post is not indexed.

Important! Relevanssi requires all filter functions attached to this hook to be proper functions. Do not use anonymous functions or class functions.

Examples

Index only posts from one category:

add_filter( 'relevanssi_indexing_restriction', 'rlv_only_index_category_x' );
function rlv_only_index_category_x( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.ID IN (
      SELECT tr.object_id FROM $wpdb->term_relationships AS tr, $wpdb->term_taxonomy AS tt
      WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
      AND tt.term_id = X ) "; // replace X with the category ID
	$restriction['reason'] .= ' Not in the right category';
	return $restriction;
}

Exclude one category:

add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_category_x' );
function rlv_exclude_category_x( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.ID NOT IN (
      SELECT tr.object_id FROM $wpdb->term_relationships AS tr, $wpdb->term_taxonomy AS tt
      WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
      AND tt.term_id = X ) "; // replace X with the category ID
	$restriction['reason'] .= ' In the wrong category';
	return $restriction;
}

Exclude products where the discontinued custom field is set to 1:

add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_discontinued' );
function rlv_exclude_discontinued( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.ID NOT IN (
      SELECT post_id FROM $wpdb->postmeta
      WHERE meta_key = 'discontinued' AND meta_value = '1' ) ";
	$restriction['reason'] .= ' Discontinued';
	return $restriction;
}

Exclude attachments with a specific file name:

add_filter( 'relevanssi_indexing_restriction', 'rlv_bulletin_restriction' );
function rlv_bulletin_restriction( $restriction ) {
	global $wpdb;
	$restriction['mysql']  .= " AND post.ID NOT IN (
      SELECT post_id FROM $wpdb->postmeta
      WHERE meta_key = '_wp_attached_file' AND meta_value LIKE '%bulletin.pdf' ) ";
	$restriction['reason'] .= ' Bulletin files';
	return $restriction;
}

Default behaviour

All SEO plugin noindex compatibility is added with this filter hook. Also, the Relevanssi Premium “exclude this post from the index” functionality is implemented with this filter hook.

Use cases