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 ){
        $post_url = get_permalink();
        wp_redirect( $post_url );
} add_action('template_redirect', 'redirect_to_post');

Comments  Leave a Reply

  1. not sure where on paste the code in function .php 🙁

  2. Worked like a charm. Excellent and Thank you.

  3. WordPress Function Redirect to Single Post in Category or Taxonomy | Ellen Jane Moore April 27, 2012 at 10:33 am

    […] A client requested to have a site visitor redirected to the single post if there is only one post in a custom taxonomy.  Thanks to Kevin Chard over at WPSNIPP.COM I found this snippet which redirects user to the post page when the category or tag has only one post (original post). […]

  4. great & Cool !!! thanks…

    1. Glad you like the snippet enjoy Adm,

  5. Your snippet is great !!!

    1. Cool thanks  Riversatile glad you like it.

  6. Hey there Kevin, I am having a similar issue I believe. Big G indexed a bunch of  category archives, now I have like 8 listings in the serps.. I was looking around on how to just redirect all categories to the homepage. Its a WP blog, and I read something about htaccss. 
    Is this similar to what you posted above?Thanks mateRu

    1. Well the above script checks to see if a category has a post however you could simply check if it is an archive and then redirect to homepage.

      function redirect_to_home(){
          if( is_archive()){
      } add_action(‘template_redirect’, ‘redirect_to_home’);

      1. Michiel Holtgreve September 3, 2014 at 7:20 am

        Thanks a lot Kevin! This works also for me. Was looking for this for a long time!

  7. Anderson Luiz Narciso May 21, 2011 at 7:36 pm

    Mannn u save me (L), i was using a plugin to redirect, but is a lot of work put link by link to
    redirect, and its shape redirects all at once, very good, and thank u

    1. @google-bbb748471f941d6cd59d0e099de0729c:disqus  cool glad that I could help out.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!