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 post IDs that are indexed. 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 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 an 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 out a lot when you try to figure out why a post is not indexed.

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;
}

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