Skip Category and Tag Pages With Only One Post
Sends visitors straight to the post when a category or tag archive contains just one post, saving them a pointless extra click.
Opens WPIDE on
Code
add_action( 'template_redirect', function () {
global $wp_query;
if ( ! ( is_category() || is_tag() ) || is_paged() || is_feed() ) {
return;
}
if ( 1 === (int) $wp_query->found_posts && ! empty( $wp_query->posts[0] ) ) {
wp_safe_redirect( get_permalink( $wp_query->posts[0] ), 302 );
exit;
}
} );
Notes
Uses a temporary redirect, so the archive works normally again as soon as a second post is added.
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.