How to Redirect if Category or Tag Contains Only One Post WordPress
I was doing some research on wordpress tags and categories and the below script which is needed for redirecting single posts related categoies and tags to be redirected to the post itself in wordpress. Below code is wordpress redirect without plugin needed. it does not add more overhead on server performance.
Below code can be added to child theme(recommended) or to your theme’s functions.php file
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’);
… Read the rest