Skip to content

Instantly share code, notes, and snippets.

@herbie4
Last active March 18, 2020 13:09
Show Gist options
  • Save herbie4/319ed406c8a33ae96604a26bf5dc6da8 to your computer and use it in GitHub Desktop.
Save herbie4/319ed406c8a33ae96604a26bf5dc6da8 to your computer and use it in GitHub Desktop.
WordPress: Set metatag to no index for older posts
<?php
// WordPress: set no index for older posts in WordPress
// -----------------------------
// check the post age
// returns true if older than...
function hhdev_is_old_post($days = 730) {
// 2 years old = 730
$days = (int) $days;
$offset = $days*60*60*24;
if ( get_post_time() < date('U') - $offset ) return true;
return false;
}
// add Meta Tags in post when older than 2 years
function hhdev_add_age_meta_tags() {
if( is_single() && hhdev_is_old_post() ) echo '<meta name="robots" content="noindex,follow" />';
}
add_action('wp_head', 'hhdev_add_age_meta_tags');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment