Skip to content

Instantly share code, notes, and snippets.

@blogjunkie
Created September 4, 2023 08:46
Show Gist options
  • Save blogjunkie/75820791e439d45ece8d132145a698cf to your computer and use it in GitHub Desktop.
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
<?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