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">';
} ?>
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">';
} ?>
Share
Improve this question
asked Feb 19, 2020 at 18:44
TnaceTnace
173 bronze badges
1 Answer
Reset to default 1From 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;
}
}