Posted on

Deindexing Yoast SEO noindex posts

If you use Yoast SEO to mark posts “noindex”, you may want to have Relevanssi deindex those posts as well. It’s easy to do using the relevanssi_do_not_index hook. Just add this function to your theme functions.php:

add_filter('relevanssi_do_not_index', 'rlv_yoast_noindex', 10, 2);
function rlv_yoast_noindex($do_not_index, $post_id) {
	$noindex = get_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', true);
	if ($noindex) $do_not_index = true;
	return $do_not_index;
}

Once you’ve added the code, noindex posts will be removed from the index when you rebuild the index or save the post.

  • Duncan Elsey

    In the lastest version of Yoast SEO the _yoast_wpseo_meta-robots-noindex is not binary. If you set a post/page/custom object to no index but later change it back to index the value of the _yoast_wpseo_meta-robots-noindex meta data is set from 1 (no index) to 2 (presumably index). The code snippet still excludes this from the Relevanssi index because the meta key is still there. I have updated the code snipped in my function.php as follow, and this now includes pages that were previously no-indexed and then set back to index.

    function rlv_yoast_noindex($do_not_index, $post_id)
    {
    $noindex = get_post_meta($post_id, ‘_yoast_wpseo_meta-robots-noindex’, true);
    if ($noindex==1) $do_not_index = true;
    return $do_not_index;
    }