[wd_asp elements=’search’ ratio=’100%’ id=1]

Stop archives from being indexed

7th July 2020

Wordpress - Miscellaneous

wordpress category

There are a few ways, in my opinion, stopping archives from being indexed, I use the following code, it seems to work well.
This will also get rid of the annoying ‘author’ also.


/* Register template redirect action callback */
add_action('template_redirect', 'codehaven_remove_wp_archives');

/* Remove archives */
function codehaven_remove_wp_archives(){
//If we are on category or tag or date or author archive
if( is_category() || is_tag() || is_date() || is_author() ) {
global $wp_query;
$wp_query->set_404(); //set to 404 not found page
}
}


/* Register template redirect action callback */
add_action('template_redirect', 'codehaven_remove_wp_archives');

/* Remove archives */
function codehaven_remove_wp_archives(){
//If we are on category or tag or date or author archive
if( is_category() || is_tag() || is_date() || is_author() ) {
global $wp_query;
wp_redirect( home_url( '/' ) );
// $wp_query->set_404(); //set to 404 not found page
}
}