Hide my staging subdomains from search results without impacting the visibility of the root domain

Use a Plugin

First of all, your CODE looks fine. However, it’s better if you put that code in a small custom Plugin. That way, you can easily keep the plugin active on the development and staging sites and deactivate it on the main site.

Changing the themes back and forth in the staging and production is not a good idea. It may accidentally de-index your main site.

Also, we should have debugging enabled on development and staging sites. Often it’s done by having different wp-config.php files (excluded from Git) on development, staging and production sites.

You may even take advantage of that and set the following true on dev and staging, and false on production:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );

Then never set these to true on production, at least not WP_DEBUG_DISPLAY. This way, you can just use the plugin and forget it’s even there. No further change is needed from staging to production unless you change WP_DEBUG_DISPLAY on production (which you shouldn’t do anyway), neither on the files, nor on the database or admin settings.

Here’s how the plugin code would look like:

<?php
/*
    Plugin Name: Custom No Index
    Description: A plugin to add a noindex, nofollow meta tag to the head of every page.
    Author: Fayaz Ahmed
    Version: 1.0
*/

add_action( 'wp_head', 'no_index_on_debug' );
function no_index_on_debug() {
    if( WP_DEBUG && WP_DEBUG_DISPLAY ) {
        ?>
        <meta name="robots" content="noindex, nofollow" />
        <?php
    }
}

Use Google Search Console

Secondly, you may also use Google Search Console to expedite the process of de-indexing your staging site’s pages. Here are the steps you can take:

  1. Go to https://search.google.com/search-console/ and sign in with your Google account.

  2. Select the property (website) that represents your staging site from the list of properties.

  3. From the left menu go to: IndexingRemovals, and in the Temporary Removals Tab, use the NEW REQUEST button to submit URLs for removal.

It may take some time for Google to process the request to remove the URLs from search results. You can also use the “URL inspection” tool in Search Console to check the status of your request and see if the URL has been removed from the search results.