How to Hide Single Posts of a Custom Post Type in WordPress

It’s a pretty common situation in WordPress where you’re using a custom post type for, say, a feed on a page, but you don’t want individual posts of that post type visible as separate pages.

One example of this is a staff page. For ease of maintenance, you might want to create a custom post type for staff members, then display those custom posts on a staff page. But, you don’t want each staff member’s post to be accessible individually through a URL.

This was the case on a corporate site that I was working on when employees’ custom post type pages started showing up in Google search results.

Here are two ways to hide single posts of a custom post type.

1. The Easiest Solution

If you’re using the Advanced Custom Fields or Custom Post Type UI plugins, these settings will hide single post URLs:

  • Set Publicly Queryable to FALSE
  • Set Has Archive to FALSE
  • Set Exclude From Search to TRUE

Give this a try first.

2. The Redirect Solution

There are some corner cases where those settings might go too far and not be usable for you.

In that case, you can redirect the single posts by using a code snippet in your theme’s functions.php file as follows:

// -------------------------------------------------
// REDIRECT STAFF SINGLE POSTS
// https://developer.brianshim.com/hide-single-posts-of-a-custom-post-type/
// -------------------------------------------------
function cpt_redirect_post() {
  if ( is_singular( 'staff' ) ) {
    wp_redirect( home_url(), 301 );
    exit;
  }
}
add_action( 'template_redirect', 'cpt_redirect_post' );

Using our staff example, this code assumes you have a custom post type called “staff”, which you display on a page. But, you don’t want individual staff pages to be visible on the front end. This code simply redirects all “staff” posts to the home page. You can redirect to a 401 page if you prefer.

Remember to also remove these custom post types from your sitemap so they don’t show up in Google search. In Yoast, this can be done in Search Appearance -> Content Types -> Show Posts in search results (set to off).

3. An Alternate Solution for Divi

If you are using the Divi theme, you could accomplish this by using the Divi Builder. If you want to redirect all custom posts to an archive page for example, create a template for the Archive page in Divi Builder, and apply that template not only to the archive but to all posts of that type. When someone visits any post, the archive page will be displayed. (This solution from Jason Miller in the Divi Engine Users Facebook page).

I hope this was helpful to you. Let me know if you have comments or questions below! – Brian

Shares

Share This Article

Please Leave a Question or Comment

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Inline Feedbacks
View all comments