Noindex posts only for google without plugins

Hi. I’m trying to allow noindexing posts and pages (for Google) through a custom field using this code:

$noindex = get_post_meta($post->ID, 'noindex-page', true);
if ($noindex) {
    echo '<meta name="googlebot" content="noindex,follow" />';
}

The code needs to be in the header, so I’m setting the location in WPCode to “Site Wide header” and activated it.
I’m creating the “noindex-page” custom field in the WordPress editor and putting a value in it for the desired post, but nothing occurs when checking the source code of the post.

I’m wondering if i shouldn’t maybe create the custom field by code; could you please advise me on how to solve this issue?

Thank you very much.

PS: I’m using:
– WordPress: 6.4.3.
– GeneratePress Theme: 3.4.0.
– WPCode Lite: 2.1.10.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer