Set NOINDEX for all posts in a category

#101878
  • Resolved Dietrich
    Rank Math free

    Parusing the forums, I’ve found that one can set NOINDEX by post type using the following:

    /**
     * Allows filtering of the robots meta data.
     *
     * @param array $robots The meta robots directives.
     */
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
         global $post;
         // filter for only a specific post type
         if(get_post_type() == "your_post_type_name_here"){
            $robots['index'] = "index";
            return $robots;
         }
    	return $robots;
    });

    Is there a way to change this code (and I think I know where, just not what) to set this to disable indexing on ALL posts within a category?

    Thanks

Viewing 1 replies (of 1 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    I am really sorry for this late reply. Yes, you could use a code like this one:

    /**
     * Allows filtering of the robots meta data.
     *
     * @param array $robots The meta robots directives.
     */
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
         global $post;
    
         $postInCategory = false;
         foreach((get_the_category()) as $category){
            if($category->name == "category name" /* you could add more categories by modifying this if */)
            {
               $postInCategory = true;
               break;
            }
         }
    
         if($postInCategory == true)
         {
            $robots['index'] = "noindex";
            $robots['follow'] = "nofollow";
         }
    
         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 1 replies (of 1 total)

The ticket ‘Set NOINDEX for all posts in a category’ is closed to new replies.