WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Redirect user if there's only 1 post in category/tag page ?

[Resolved] Redirect user if there's only 1 post in category/tag page ?

  • Hi,

    Is there a plugin (or hack) that allows me to redirect users if there’s only 1 post in category/tag page ? I mean redirect them directly to this single post.

    I tried http://wordpress.org/support/topic/a-plugin-to-redirect-user-if-theres-only-1-post-in-categorytag-page?replies=2

    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');

    Works fine on one of my blog. But on my other blog writes “Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.” What’s the problem?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Redirect user if there's only 1 post in category/tag page ?’ is closed to new replies.