Skip to content

Instantly share code, notes, and snippets.

@dibakarjana
Created July 14, 2022 16:08
Show Gist options
  • Save dibakarjana/b1f6a0c843533619168e10d7da772577 to your computer and use it in GitHub Desktop.
Save dibakarjana/b1f6a0c843533619168e10d7da772577 to your computer and use it in GitHub Desktop.
Wordpress - Redirect any page To Single Post Page or any other variable url
function redirect_to_single_post(){
global $wp_query;
if((is_archive() || is_category()) && $wp_query->post_count == 1){
// Setup post data
the_post();
// Get permalink
$post_url = get_permalink();
// Redirect to single post page
wp_redirect( $post_url );
}
}
add_action('template_redirect', 'redirect_to_single_post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment