Setting a Specific Page to No Index via PHP theme code

#810547
  • Resolved Joshua Larson
    Rank Math free

    Hello there,

    I am a developer working with our application. I am looking for a way that we can set a specific page (by post ID) to No Index. Does a method exist for this application?

    Use case: Create a new page on the website with some Advanced Custom Fields. Depending on some of the ACF fields, dynamically set certain pages to “No Index”

    Thank you for the help. I look forward to your response.
    Josh

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention.

    Apologies for the delay as this ticket seems to have been stuck in our drafts section for some reason.

    You can use and customize this filter to noindex URLs using the post ID:

    add_filter('rank_math/frontend/robots', function ($robots) {
    global $post;
    if( $post->ID == 558){ // add the post ID and the condition from your ACF
    $robots['index'] = "noindex";
    		$robots["follow"] = 'nofollow';
    		return $robots;
    	}
    	return $robots;
    });

    You may refer to this guide on how to add filters to your website:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps.

    Thank you.

    Hello Reinelle,

    Thank you for the help! That worked very well for us. Just to confirm, this solution will stop any Google crawlers from indexing these pages? I would think with the filter, the robots would only render on an actual page load. Does a bot crawler also see the noindex when it is being dynamically set in this way?

    Thank you again for the help,
    Josh

    Hello,

    Yes, the Googlebot and other crawlers will see this page as noindex because this is added when the page is loaded.

    You can check the same by looking at the source code of the live page.

    Don’t hesitate to get in touch if you have any other questions.

    Very good! Thank you for confirming.

    Have a wonderful day!
    Josh

    Hello,

    We are glad to know that this issue has been resolved. Thank you for letting us know.

    This ticket will be closed now, but you can always open a new one if you have any other questions or concerns. We are here to help you with anything related to Rank Math.

    We appreciate your patience and cooperation throughout this process.

    Thank you for choosing Rank Math.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘Setting a Specific Page to No Index via PHP theme code’ is closed to new replies.