Exclude no-index content from WordPress search

/** * Exclude noindex'd pages from site search * */ function be_exclude_noindex_from_search( $query ) { if( $query->is_main_query() && $query->is_search() && ! is_admin() ) { $meta_query = isset( $query->meta_query ) ? $query->meta_query : array(); $meta_query['noindex'] = array( 'key' => '_yoast_wpseo_meta-robots-noindex', 'value' => 1, 'compare' => 'NOT EXISTS', ); $query->set( 'meta_query', $meta_query ); } } add_action( 'pre_get_posts', 'be_exclude_noindex_from_search' ); /** * Exclude noindex'd pages from SearchWP search * */ function be_exclude_noindex_searchwp( $ids ) { $loop = new WP_Query( [ 'post_type' => [ 'post', 'page' ], 'posts_per_page' => 999, 'fields' => 'ids', 'meta_query' => [ [ 'key' => '_yoast_wpseo_meta-robots-noindex', 'value' => 1, ] ] ]); if( $loop->posts ) $ids = array_merge( $ids, $loop->posts ); return $ids; } add_filter( 'searchwp_exclude', 'be_exclude_noindex_searchwp' );
Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk