1

I have a standard WordPress page category with tag_ID=92 which I want to noindex all posts in this category entirely. Is there a way to do it with actions/hooks in functions.php?

3
  • if you're using yoast already, why not just change the setting in yoast to do this for you for that category?
    – rudtek
    Commented Sep 9, 2016 at 16:26
  • This only sets noindex to the category archive and not the pages inside the category. Commented Sep 9, 2016 at 16:28
  • 1
    then create a cpt for those pages and hide them in post type settings of yoast?
    – rudtek
    Commented Sep 9, 2016 at 17:24

4 Answers 4

6

As the previous code I posted didnt work for the OP, clutching at straws, we can try to obtain the same outcome using get_the_category As the OP stated he was using YOAST, i'll wrap this function into the YOAST hook for robots.

add_filter('wpseo_robots', 'yoast_no_home_noindex', 999); 
function yoast_no_home_noindex($string= "") {
   $term_id = get_the_category( $post->ID );
      if($term_id[0]->term_id == 92) {
         $string= "noindex, nofollow";
      }
      return $string; 
}

Same again, just drop this into your themes functions file.

10
  • This time we have the meta tag on every page and post. It doesn't appear only on category 92, but on all pages of the site. :S Commented Sep 9, 2016 at 15:54
  • 1
    @File_Submit, me rushing, sorry.. should work fine now ..
    – Malisa
    Commented Sep 9, 2016 at 16:03
  • I'm sorry to tell, but still not working :/ Commented Sep 9, 2016 at 16:11
  • 2
    Then there lies the problem,it would seem the plugin or code you have used to add categories to pages, isnt woking correctly. You should get an output of the term associated with the page OR post
    – Malisa
    Commented Sep 9, 2016 at 16:46
  • 2
    The way I see that is this piece of code is registering a taxonomy to pages called ''taxonomy'=>'page_category'' and not category. hence the issue of why none of the core WP functions is working. Shouldnt do this here as this is dragging on.. but I would simply use the following to register category tax to pages. then it will work.: add_action('admin_init', 'reg_tax'); function reg_tax() { register_taxonomy_for_object_type('category', 'page'); add_post_type_support('page', 'category'); }
    – Malisa
    Commented Sep 9, 2016 at 17:13
3

By noindex, I'm assuming you mean meta robots noindex, if so you could manually do this by utilizing the in_category function adding the following to your theme's header between the <head></head> tags, like so::

 <?php if(in_category( '92' ))
      echo "\t<meta name='robots' content='noindex, nofollow' />\r\n" ?>

Or, If you don't care to alter your theme directly, you could attach it to the wp_head action hook, place the following in your functions.php file usually located within your main theme or child themes folder.:

add_action('wp_head', 'noRobots');
function noRobots() {
    if(in_category( '92' )) echo "\t<meta name='robots' content='noindex, nofollow' />\r\n";
}

Both the above will return true if the current post is in the category with ID '92', can also be used with category name or slug, or an array of either.

11
  • Yes, I meant meta robots noindex. Unfortunately, your code doesn't work :( By the way, I am using Yoast plugin if that matters. Commented Sep 9, 2016 at 9:41
  • I've edited the answer, my bad, try in_category, instead of is_category
    – Malisa
    Commented Sep 9, 2016 at 10:03
  • 1
    @Malisa you should really make it a fully useful code and answer.... use wp_head, explain where to place it and such. Commented Sep 9, 2016 at 12:30
  • @MarkKaplun, taken onboard what you say and added further description and detail to answer
    – Malisa
    Commented Sep 9, 2016 at 13:45
  • Hmm, weird - neither way appears to generate the mata tag. I've tried on fresh Twenty sixteen theme with no plugins. Commented Sep 9, 2016 at 14:37
0

This question is old but answer can be updated. If you use WordPress version 5.7.0 or newer, you can use filter hook wp_robots.

add_filter('wp_robots', 'mypluginRobotsFilter', 10, 1);
function mypluginRobotsFilter(array $robots)
{
    // use condition(s) check here to set "noindex"
    if (in_category('92')) {
        $robots['noindex'] = true;
    }
    return $robots;
}

This filter is used in many functions. See their documents as reference:
https://developer.wordpress.org/reference/functions/wp_robots_no_robots/
https://developer.wordpress.org/reference/functions/wp_robots_noindex/
https://developer.wordpress.org/reference/functions/wp_robots_noindex_search/

-2

in your html header add the following code :

<?php if (is_category('92')): ?>
<meta name="robots" content="noindex,nofollow">
<?php endif ?>
2
  • A little explanation of what the code does would be great. Remember that answers shoulud be useful for as many people as possible.
    – cybmeta
    Commented Sep 9, 2016 at 11:00
  • The code should add meta robots noindex to all posts inside the category 92. Commented Sep 9, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.