Created
July 31, 2018 11:03
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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