0

I want to redirect any single post to its taxonomy term's archive page if it has a specific term assigned to it.

So, if a post of a term "series" at:
domain/series/post/
I want to redirect that single post back to:
domain/series/

I've tried working with php's header() function, but I know that needs to be used before any other headers are sent. I can't seem to detect the terms AND use header() in the same place.

Any help is greatly appreciated!
Thanks!

2 Answers 2

0

There is a template_redirect hooks which is perfectly suitable for task like this.

  1. Hook into template_redirect
  2. Check your context (is_single(), etc)
  3. Redirect with wp_safe_redirect() (if it's inside a site)
  4. die() to prevent execution from proceeding
8
  • Is it safe to run those functions from any file, or do I have to do that within functions.php?
    – Mattaton
    Apr 13, 2015 at 16:31
  • It doesn't matter technically, as long as you don't attempt it too early or something. functions.php is perfectly acceptable. Note the actual execution should be hooked to template_redirect.
    – Rarst
    Apr 13, 2015 at 16:32
  • Okay, the best place would be to do it as I'm already detecting the terms for other purposes in the single.php file. I'll try it there and see what happens. Thanks!
    – Mattaton
    Apr 13, 2015 at 16:34
  • Template file is too late for this. template_redirect is happening before template choice and load.
    – Rarst
    Apr 13, 2015 at 16:34
  • 1
    Thanks! Got it working. I can post my code here as an answer if anyone would find it useful.
    – Mattaton
    Apr 13, 2015 at 17:45
2

Thanks to Rarst for the tip.
Here is the code I came up with to accomplish redirects for two similar taxonomy/post type set ups.
This basically does what I indicated in the OP. If I hit a single post with a top-level term assigned to it, the url will go from
domain/series/post/
to
domain/series/

It simply strips the post's slug from the end of the url.

Obviously, the CPTs, taxonomies and rewrites have to be set up properly to work with this.

I added the $type parameter to the theme_perform_redirect() function so that I can add functionality to it later for other types of redirects.

function theme_perform_redirect($post, $taxonomy, $type) {
    if ($type == 'top-level') {
        $top_level_terms = get_top_level_term_ids( $taxonomy );
        $post_terms = wp_get_post_terms( $post->ID, $taxonomy );
        if ( in_array($post_terms[0]->term_id, $top_level_terms)) {
            // This is the main/top post, redirect it to the archive
            $to_strip = $post->post_name;
            $permalink = get_permalink( $post->ID );
            $go_here = str_replace($to_strip."/", "", $permalink);
            wp_redirect( $go_here );
            exit();
        }
    }
}

function theme_redirects() {
    global $post;
    if ( is_single() ) {
        if (is_singular( 'cartoon-series' )) {
            heman_perform_redirect($post, 'cartoon-features', 'top-level');
        } else if (is_singular( 'movies' )) {
            heman_perform_redirect($post, 'movie-features', 'top-level');
        }
    }
}
add_action( 'template_redirect', 'theme_redirects' );

Note that get_top_level_term_ids() is my own function. It just grabs all the terms with a parent of 0....just in a neater package. :-)

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.