How can I hide all posts from a custom post type from search engines and local search?

Hi,
I know that there are plugins such as SmartCrawl that help us hiding single posts from search engines and wp-search.
So now I’d like to hide all posts from a custom post type, even better without SmartCrawl (cannot use it on German sites).
How to achieve this?
Thank you and kind regards,
Sascha

  • Adam Czajczyk
    • Support Gorilla

    Hi sascha

    I hope you’re well today!

    SmartCrawl would let you exclude entire post type from indexing by Google and other search engines but if you can’t use SEO plguin for that, then there are other ways to achieve that in other ways to.

    The simplest one would be to use robots.txt file rules if all those custom posts are available under some common URL/slug. For example

    yoursite.com/my-post-type/example-post1
    yoursite.com/my-post-type/example-post2

    and so on.

    In such case you could modify (or add) robots.txt file to disallow access to those URLs liek that

    User-agent: *
    Disallow: /my-post-type

    Another way is to “inject” a meta tag to these custom posts only. A code like this (added to functions.php of theme or as MU plugin) should do the trick:

    function wpmu_inject_custom_metadata() {
    
        global $post;
    
        if ( is_singular( 'my_post_type' ) ) {
    
      ?>
    
        <meta name="robots" content="noindex, nofollow" />
    
      <?php
      
    	}
    
    }
    add_action( 'wp_head', 'wpmu_inject_custom_metadata' );

    Note: you’ll have to replace “my_post_type” with your custom post type.

    As for hiding them form WP search – if you can’t modify custom post type registration code, then this snippet should help too:

    function remove_post_type_page_from_search() {
        global $wp_post_types;
        $wp_post_types['my_post_type']->exclude_from_search = true;
    }
    add_action('init', 'remove_post_type_page_from_search');

    Again, you’ll have to replace “my_post_type” with your custom post type.

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hi sascha

    I’ve shared three pieces of code there and they all are quite “lightweight” but

    1) the first one is for “robots.txt” file and cant be added via such plugin; you could try adding it via our SmartCrawl if you’re already using it, as it has “Robots.txt editor” built-in. But this code would only stop search engines. Personally, I’d go for the second and third code.

    2) Second and third pieces of code are not “interchangeable” – they are doing two different things. First one stops search engines’ robots from indexing the site, the second one removes your custom posts from results of your WP search on your site.

    Best regards,
    Adam