To keep one WordPress post or page out of Google, give that post a noindex robots meta tag. You can set it with your SEO plugin’s per-post setting or with the short wp_robots snippet below. To hide the whole site, tick one box under Settings → Reading.
Both methods only work if search engines can still crawl the page, so don’t also block it in robots.txt. The last section explains why.
Hide the whole site: Settings → Reading
Go to Settings → Reading, tick Discourage search engines from indexing this site next to “Search engine visibility”, and save.
On WordPress 7.1 that setting does three things. Every page gets this tag in its <head>:
<meta name='robots' content='noindex, nofollow' />Code language: HTML, XML (xml)
The built-in XML sitemap at /wp-sitemap.xml returns a 404, and the Sitemap: line disappears from the virtual robots.txt. What it does not do is add Disallow: / to robots.txt. WordPress stopped doing that in version 5.3, because a crawler that is blocked from a page never sees its noindex tag.
WordPress’s own description under the checkbox is accurate: “It is up to search engines to honor this request.” Google respects noindex, but it is a request, not a lock. Other bots can ignore it, and anyone who has the URL can still open the site. For a staging or private site that must stay hidden, put it behind a login. Google’s own advice is to “block indexing with noindex or password-protect the page” (Google Search Central). This snippet makes every visitor log in first.
If you used this setting while building the site, untick it when you launch. Otherwise the whole site keeps telling search engines not to index it.
Noindex one post or page with a custom field
Older tutorials, including the first version of this one, told you to paste a get_post_meta() check into your theme’s header.php that echoed a robots meta tag. That no longer works well. Block themes such as Twenty Twenty-Five have no header.php, edits to theme files are lost when the theme updates, and since WordPress 5.7 core prints its own robots meta tag through the wp_robots API. Hard-coding a second tag gives the page two of them.
The fix is to hook into that API. Your directive is then merged into the single tag WordPress already outputs. This snippet keeps the noindex-post custom field name from the original tip, so posts you marked years ago are covered as soon as you add it:
// Noindex any post or page that has a "noindex-post" custom field with a value.
add_filter( 'wp_robots', function ( $robots ) {
if ( is_singular() && get_post_meta( get_queried_object_id(), 'noindex-post', true ) ) {
$robots['noindex'] = true;
}
return $robots;
} );
// Keep the same posts out of the built-in XML sitemap (wp-sitemap.xml).
add_filter( 'wp_sitemaps_posts_query_args', function ( $args ) {
$args['meta_query'] = array(
'relation' => 'OR',
array( 'key' => 'noindex-post', 'compare' => 'NOT EXISTS' ),
array( 'key' => 'noindex-post', 'value' => '' ),
);
return $args;
} );Code language: PHP (php)
The second filter removes the same posts from WordPress’s built-in sitemap, so you aren’t asking Google to crawl URLs you have told it not to index.
To mark a post, you need the Custom Fields panel, which the block editor hides by default:
- Open the post and click the Options menu (the three dots, top right).
- Go to Preferences → General, and under Advanced turn on Custom fields, then click Show & Reload Page.
- Below the content, open the Meta Boxes area. In Custom Fields, click Enter new, type
noindex-postas the name and any value, such asyes, then click Add Custom Field.
After that the post’s HTML contains a single robots tag:
<meta name='robots' content='max-image-preview:large, noindex' />Code language: HTML, XML (xml)
max-image-preview:large is a directive WordPress adds to every page by default. It does no harm next to noindex. To make the post indexable again, delete the custom field or empty its value.
If you still have the old code in header.php, remove it after adding this snippet, or the page will carry two robots tags. The original tip also added nofollow. You don’t need it: noindex alone keeps the page out of search results, and without nofollow Google can still follow its links. If you want it anyway, add $robots['nofollow'] = true; below the noindex line.
Noindex a list of posts by ID
If you would rather keep the list in code than in custom fields, use post IDs. You can find a post’s ID in the address bar while editing it (post.php?post=123&action=edit).
// The IDs of the posts and pages to keep out of search results.
$noindex_ids = array( 42, 128, 356 );
add_filter( 'wp_robots', function ( $robots ) use ( $noindex_ids ) {
if ( is_singular() && in_array( get_queried_object_id(), $noindex_ids, true ) ) {
$robots['noindex'] = true;
}
return $robots;
} );
add_filter( 'wp_sitemaps_posts_query_args', function ( $args ) use ( $noindex_ids ) {
$args['post__not_in'] = $noindex_ids;
return $args;
} );Code language: PHP (php)
Replace the three example IDs with your own. Because the check uses is_singular(), it works for posts, pages and custom post types alike.
If you use an SEO plugin, use its setting instead
SEOPress, Yoast SEO and Rank Math all have a per-post noindex option, and they also drop the post from their own sitemaps. In each one it is in the Advanced tab of the plugin’s SEO panel in the editor. In Yoast, set “Allow search engines to show this content in search results?” to No. In Rank Math, tick No Index under Robots Meta (the Advanced tab needs Rank Math’s Advanced Mode). In SEOPress, set the meta robots to noindex. Use that option rather than the snippet. With SEOPress 10.3 active, the snippet printed a second robots tag next to SEOPress’s own index, follow tag. Google applies the more restrictive of conflicting rules, but two contradictory tags are worth avoiding.
Why not just block the page in robots.txt?
noindex and a robots.txt Disallow rule do different jobs, and combined they work against each other. A Disallow rule stops crawling, not indexing. If other pages link to a blocked URL, Google can still index it and show it in results without a description. Worse, if a noindexed page is also blocked in robots.txt, Google never fetches it, so it never sees the noindex. In Google’s documentation, the page “must not be blocked by a robots.txt file” for noindex to work, and putting noindex inside robots.txt is not supported. Images are different: blocking Googlebot-Image in robots.txt is one of the two ways Google documents for keeping images out of Google Images.
A page that is already in Google drops out only after Googlebot recrawls it, which Google says can take months. To speed that up, request a recrawl with the URL Inspection tool in Search Console. If a page has to disappear right away, use Search Console’s Removals tool as a temporary measure while the noindex takes effect.
Where to add this code
Add the snippet with a code snippets plugin instead of your theme’s functions.php, so it survives theme updates and you can switch it off without FTP. This step-by-step guide to safe code snippets uses the free Code Snippets plugin. On my own sites I use WPCodeBox, which keeps snippets in a cloud library so the same snippet can go on several sites. Not sure which plugin to choose? I compared Code Snippets, WPCode and WPCodeBox by feeding each one broken snippets. (Marius)
Disclosure: the WPCodeBox link is an affiliate link. If you buy through it, I earn a commission at no extra cost to you.
Hi.
I’m trying to use this code for noindexing some posts on my website, but it doesn’t seem to work.
Is it still usable?
My config:
– WordPress: 6.4.3.
– GeneratePress Theme: 3.4.0.
– WPCode Lite: 2.1.10.
Thank you very much.