Add noindex to pages that shouldn't appear in search results, such as thank-you pages and members-only areas.
add_filter( 'wp_robots', function ( array $robots ): array {
// Page slugs to keep out of search results.
$noindex = [ 'thank-you', 'order-received', 'members' ];
if ( is_page( $noindex ) ) {
$robots['noindex'] = true;
$robots['follow'] = true;
}
return $robots;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Thank-you pages, confirmation pages and private areas sometimes end up in Google, where they confuse searchers and inflate conversion tracking. This snippet tells search engines not to index the pages you list.
WordPress builds its robots meta tag through the wp_robots filter. For pages matching one of the slugs, the snippet adds noindex, follow: search engines drop the page from results but still follow its links.
is_page() also accepts IDs and titles, so [ 42, 'thank-you' ] works too.View the source of a listed page and look for name='robots'. It should include noindex.