Return 404 for Empty Category and Tag Pages
Shows your "not found" page with a 404 status for categories, tags and other terms that have no published posts, instead of an empty archive.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
global $wp_query;
if ( ( is_category() || is_tag() || is_tax() ) && ! is_paged() && ! $wp_query->have_posts() ) {
$wp_query->set_404();
status_header( 404 );
nocache_headers();
}
}, 1 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.