WordPress – Redirect Default Archive Pages

WordPress - General Functions PHP

Snippet Overview

WordPress – Redirect Default Archive Pages

When you build your own blog or custom post type archive pages using a page builder you often don’t want visitors or more importantly search engines to find and/or index the default WordPress archive pages. Although you can configure your custom post type to disable the archive you can’t easily do this for the Posts section that comes pre-installed with WordPress. This function allows you to easily redirect those category tag date and author archive pages to a 404 page so they are not visible to users.

To enable this function just add the following code to your WordPress theme’s functions.php file.

// Redirect default WordPress archive pages

function redirect_default_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
	}
}

add_action('template_redirect', 'redirect_default_wp_archives');

You can also redirect to a specific page by adding the following to your theme’s functions.php file. Just be sure to swap out the example url with your site-specific url.

// Redirect default WordPress archive pages

function redirect_default_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->wp_redirect( 'http://www.example.com', 301 ); exit;
	}
}

add_action('template_redirect', 'redirect_default_wp_archives');
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply