Prevent Custom Post Type from Being Found (by URL)

Sometimes Custom Post Types are built to organize and filter information, such as creating a Research archive or a Staff directory.  That content is meant to be seen on the front end in the single.php template.

However, when creating a Custom Post Type for a rotating Banner, or a single page listing all of your Testimonials, the single.php template is moot.

You can easily get around this using a plugin like Page Links To for your carousel post types. But for Testimonials, where the content doesn’t redirect to other areas of the website, it’s more pertinent to setup a template_redirect:

add_filter('template_redirect', 'term_link_filter', 10, 3);
function term_link_filter( ) {
	
    if( is_singular('banner') ) {
        wp_redirect( home_url(  ) );
        exit();
    } else if( is_singular('testimonial') ) {
        wp_redirect( home_url( '/testimonials' ) );
        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.