Created
September 4, 2023 08:46
-
-
Save blogjunkie/75820791e439d45ece8d132145a698cf to your computer and use it in GitHub Desktop.
Exclude pages that have been noindex-ed in Rank Math from WP search
This file contains 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 // Do not copy this line | |
function child_exclude_noindex_from_search( $query ) { | |
if( $query->is_main_query() && $query->is_search() ) { | |
$meta_query = isset( $query->meta_query ) ? $query->meta_query : array(); | |
$meta_query['noindex'] = array( | |
'key' => 'rank_math_robots', | |
'value' => "noindex", | |
'compare' => 'NOT EXISTS', | |
); | |
$query->set( 'meta_query', $meta_query ); | |
} | |
} | |
add_action( 'pre_get_posts', 'child_exclude_noindex_from_search' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment