Skip to content

Instantly share code, notes, and snippets.

@uprise10
Created July 31, 2018 11:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uprise10/a0ebb892b99011aee58637ab0bbcac43 to your computer and use it in GitHub Desktop.
Save uprise10/a0ebb892b99011aee58637ab0bbcac43 to your computer and use it in GitHub Desktop.
This piece of code excludes posts from the WordPress search which have the robots meta settings on noindex, following the Yoast SEO settings.
<?php
add_action( 'pre_get_posts', function( \WP_Query $query ) {
if( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$meta_query = [
'relation' => 'OR',
[
'key' => '_yoast_wpseo_meta-robots-noindex',
'value' => '1',
'compare' => '!=',
],
[
'key' => '_yoast_wpseo_meta-robots-noindex',
'value' => '',
'compare' => 'NOT EXISTS',
]
];
$query->set( 'meta_query', $meta_query );
}
return $query;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment