Redirect to a single post if one post in category or tag

Add this snippet to the functions.php of your wordpress theme. This snippet will redirect to the post page when a category or a tag archive has only one post.

 

 

function redirect_to_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_redirect( $post_url );
}
} add_action('template_redirect', 'redirect_to_post');

Leave a comment