• Resolved dev_John

    (@dev333)


    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

    • This topic was modified 4 years, 4 months ago by dev_John.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Michael Ti?a

    (@mikes41720)

    Hello,

    We can’t offer support on custom code (needed to change (core) features of our plugin). Since we don’t want to take the risk that we break your website by suggesting incorrect or incomplete code, we cannot advise you on how to make such changes. Maybe someone watching these forums can assist you further, but if your topic is inactive for 7 days, we’ll mark it as resolved to keep the overview.

    Thank you for your understanding.

    You might want to refer to our Yoast SEO API overview for more information as well — https://developer.yoast.com/customization/apis/overview

    Plugin Support devnihil

    (@devnihil)

    We are going ahead and marking this issue as resolved due to inactivity. If you require any further assistance please create a new issue.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude no-index posts/pages from WP search’ is closed to new replies.