Disabling archives pages in WordPress

Disabling archive pages for authors, dates and tags

When creating a WordPress website, you’re most likely creating a corporate website or a blog. But, when creating a corporate website or a blog, you might not need some archive pages WordPress creates for you. Disabling these pages can be a pain in the ass if your creating one of your first websites. To disable these pages, the parse_query hook is used.

The parse_query hook will be fired when WordPress is collecting data about the page that it would normally serve. Except, we’re now checking if that page is an archive we don’t want. If the requested page is a page we don’t want, we simply trigger an 404 page instead of the original page.

/**
 * Disable archive pages for author, dates and tags
 *
 * @param \WP_Query $wp_query the query to adjust
 */
function disable_unwanted_archives( $wp_query ) {
    // Add some more if you want, https://codex.wordpress.org/Conditional_Tags
	if ( is_author() || is_date() || is_tag() ) {
		$wp_query->set_404();
		status_header( 404 );
	}
}

add_action( 'parse_query', 'disable_unwanted_archives' );