How to bulk noindex old posts?

#16301
  • Resolved Peter Emad
    Rank Math free

    How can I add noindex tag to all old posts at once? I know that RankMath does this but to new posts only and that’s an issue to me because I need to add noindex tag to about 400 pages and it’d be a waste of time to do it manually.
    Is there’s anything that I can do to get this done?

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

    Thank you for contacting Rank Math today.

    I can suggest that you make use of the following filter to check the current post date and add a noindex tag on your theme’s functions.php file:

    /**
     * Allows filtering of the robots meta data.
     *
     * @param array $robots The meta robots directives.
     */
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    	return $robots;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    But how can I do so? Technically

    Hello,

    Thank you for contacting Rank Math today.

    You can create some logic to compare the post publish date with a certain cut off date that you have set for old posts as shown below:

    
    /**
     * Allows filtering of the robots meta data.
     *
     * @param array $robots The meta robots directives.
     */
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
            global $post;
            //convert the dates to unix timestamps
            $cut_off_date = strtotime( "2019-01-01" );
            $post_date    = strtotime( $post->post_date );
            //compare the current post with the cut off date
            if($post_date <= $cut_off_date){
                $robots['index'] = 'noindex';
                $robots['follow'] = 'follow';
                return $robots;
            }
    	return $robots;
    });

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘How to bulk noindex old posts?’ is closed to new replies.