Exclude no-index posts/pages from WP search
-
Hi there!
I would like to add a function by which all pages/posts (any post type) that have a “noindex” tag (I enable or disable this only via the Yoast SEO plugin) are also automatically hidden from WP search inside the site.
I thought it should be a function like this:
function dev_exclude_noindex_from_search( $query ) { if( $query->is_main_query() && $query->is_search && !is_admin() ) { $query->set( 'meta_query', array( array( 'key' => '_yoast_wpseo_meta-robots-noindex', 'value' => 1, 'compare' => '!=', ) ) ); } } add_action( 'pre_get_posts', 'dev_exclude_noindex_from_search' );
But I found that this doesn’t work because the “_yoast_wpseo_meta-robots-noindex” key might not exist at all, rather than just having one of two values (“1″ or ‘2”).
I would be happy to get a hint from you for the best way to create this function.
Thank you very much!
———-
Best regards
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Exclude no-index posts/pages from WP search’ is closed to new replies.