Redirect a Custom Post Type’s Single and Archive Pages in WordPress

You may find that you need to hide or redirect a custom post types single or archive pages or even both in WordPress. This may be the case if you are outputting a custom loop of CPTs and don’t want duplicate content or discoverable pages which you don’t want published.

In the above gist in the $args, ‘has_archive‘ is set to false which hides the archive pages and publicly_queryable is also set to false which hides the single pages.

Make sure to flush permalinks after and these pages will no longer be visible.

Alternatively redirect the single pages to their archive view…

add_action( 'template_redirect', 'prefix_redirect_single_cpt' );
// Redirect Testimonials CPT Singles
function prefix_redirect_single_cpt() {
    if ( is_singular( 'testimonial' )) {
        wp_redirect( '/testimonials', 301 );
        exit;
    }
}

So just change your registered CPT type and redirection URL

2 comments

  • is it possible to just hide archives by role instead all archives eg hide subscriber archive pages and leave public author archive pages

  • Andrew Macaulay-Brook

    Hi, setting publicly_queryable to false will prevent showing both single posts and the post type archive.

Leave your comment