Redirect To Single Post Page

This article is about template redirecting in WordPress using template_redirect hook. This is a very simple but very handy for a WordPress blogger who wants to redirect the user to the single post template/full post view (even if there is only index.php) when there is only one post in the archive/category/tag. It’s helpful to a new blog site when there is a few articles like mine. I’ve found this handy and using it on my site, so I think someone else will like it too. Simply paste this code snippet in the functions.php and you are done.

function redirect_to_single_post(){
    global $wp_query;
    if((is_archive() || is_category()) && $wp_query->post_count == 1){
        // Setup post data
        // Get permalink
        $post_url = get_permalink();
        // Redirect to single post page
        wp_redirect( $post_url );

add_action('template_redirect', 'redirect_to_single_post');

Latest Blog


PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]


New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]