Question

I'm trying to noindex posts from all authors EXCEPT 3 authors on my Wordpress site.

I found the following code that I can put into the header.php file. However, this targets specific categories.

<?php if (is_single() && (in_category(array(457))))  { 
echo '<meta name="robots" content="noindex, follow">';
} ?>

How do I modify this to say something like, if is single post and is NOT an author with ID 111, 112, or 113 then insert noindex, follow. Would the following be correct?:

 <?php if (is_single() && !(is_author(array(111,112,113))))  { 
    echo '<meta name="robots" content="noindex, follow">';
    } ?>
Was it helpful?

Solution

From codex: is_author() is a conditional tag which determines whether the query is for an existing author archive page. so it does not work for your scope.

Best solution, instead of using the template file header.php is to write a function in functions.php hooking the proper action wp_head:

add_action('wp_head','AS_exclude_author_from_indexing');

function AS_exclude_author_from_indexing(){
  $toIndex = array(111,112,113);
  $user_id = get_the_author_meta( 'ID' );
  if( !in_array($user_id,$toIndex)){
    echo "<meta name=\"robots\" content=\"noindex,follow\">".PHP_EOL;
  } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top