apply_filters( 'relevanssi_do_not_index', boolean|string $block, int $post_id, WP_Post $_post )
Controls whether a post is indexed or not.
Parameters
$block
(boolean|string) If not false
, the post in question is not indexed. The value can be a boolean true
or a string that contains an explanation why the post wasn’t indexed.
$post_id
(int) The post ID.
$_post
(WP_Post) Not available yet, but later this parameter will include the whole post object.
More information
All posts are passed through this filter hook in the indexing process. If this filter hook returns a non-false
value, the post will not be indexed. If the value returned is a string, Relevanssi will then store that string in the _relevanssi_noindex_reason
custom field for the post. If the value is simply true
, Relevanssi will use “Blocked by a filter function” as an explanation.
This filter hook can be used for all kinds of exclusions that depend on the attributes of an individual post. For example, to block all posts that are published on Mondays, you could do:
add_filter( 'relevanssi_do_not_index', 'rlv_no_mondays', 10, 2 ); function rlv_no_mondays( $block, $post_id ) { $day = get_the_date( 'w', $post_id ); if ( 1 === $day ) { $block = 'I hate mondays!'; } return $block; }
To do a simple blocking based on post ID (which you can do in Premium by just checking the “Exclude this post” checkbox on the post edit page), you can use:
add_filter( 'relevanssi_do_not_index', 'rlv_post_exclusion', 10, 2 ); function rlv_post_exclusion( $block, $post_id ) { $blocked_posts = array( 1, 2, 3, 4, 5 ); // List excluded post IDs here. if ( in_array( $post_id, $blocked_posts, true ) ) { $block = 'Excluded post'; } return $block; }
Think before you use this hook
This isn’t always the best option for blocking posts. If you exclude lots of posts with relevanssi_do_not_index
, you may run into issues where the indexing process gets stuck when it only finds excluded posts and can’t progress.
If possible, it’s better to use the relevanssi_indexing_restriction
filter hook. This filter hook serves a similar purpose, but the posts excluded here are not even considered for the indexing in the first place. However, this filter function requires the restriction in the shape of a MySQL WHERE
clause, which makes some restrictions impossible to do.
For blocking small numbers of posts and cases where a MySQL query restriction is hard to do, this function will do just fine.
In Relevanssi Premium, there’s also relevanssi_do_not_index_term
that does a similar job for taxonomy terms and relevanssi_user_index_ok
for controlling whether users are indexed. For attachments, there’s relevanssi_do_not_read
.