Redirect Custom Post Types by Taxonomy Term Using Template Redirect

Based off of this WordPress codex reference function

:

Redirect Logged Out Users:

function my_page_template_redirect() {

    if( is_page_template( 'page-members.php' ) && ! is_user_logged_in() ) {

        wp_redirect( home_url( '/signup/' ) );

        exit();

    }

}

add_action( 'template_redirect', 'my_page_template_redirect' );

Redirect Custom Post Types

Use this in instances where your Custom Post Types are being used to bring data into a template page and contain small amounts of information in the individual posts.

Examples of usage with Custom Post Type:

  • Banner / carousel
  • Widget / call to action
  • Testimonials / quotes
add_filter('template_redirect', 'term_link_filter', 10, 3);

function term_link_filter( $url, $term, $taxonomy ) {

	
    // redirect single banner
	if( is_singular('banner')) {
        wp_redirect( home_url() ); // home page or URL where carousel appears
        exit();
    }
    

    // redirect single testimonial
	if( is_singular('testimonial')) {
        wp_redirect( home_url('/testimonials') ); // testimonial URL
        exit();
    }
    
    
    // redirect single widget
	if( is_singular('widgets')) {
        wp_redirect( home_url() ); // home page or URL where call to action appears
        exit();
    }

}

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.