How to disable the single view for a custom post type?

by passatgt   Last Updated December 07, 2017 11:08 AM

I created a custom post type:

register_post_type('sample_post_type',array(
    'labels' => array(
        'name' => _x('Sample Posts', 'post type general name'),
        'singular_name' => _x('Sample Post', 'post type singular name')
    ),
    'public' => true,
    'show_in_nav_menus' => false,
    'exclude_from_search' => true
));

How can i disable the single post view for this specific post type? Displaying a simple 404 is fine, or redirecting to the homepage. This is inside a plugin, so i can't create a single-sample_post_type.php file to setup an empty page.



Answers 6


You can use template_redirect hook to redirect for a custom post type, you can use any other URL you want to in place of home_url() and the error code in other argument.

<?php
add_action( 'template_redirect', 'wpse_128636_redirect_post' );

function wpse_128636_redirect_post() {
  $queried_post_type = get_query_var('post_type');
  if ( is_single() && 'sample_post_type' ==  $queried_post_type ) {
    wp_redirect( home_url(), 301 );
    exit;
  }
}
?>
sven
sven
January 05, 2014 14:12 PM

A simpler way to do that can be passing the following args when registering the Custom Post Type

register_post_type('sample_post_type',array(
'labels' => array(
    'name' => _x('Sample Posts', 'post type general name'),
    'singular_name' => _x('Sample Post', 'post type singular name')
),
'public' => true,
'exclude_from_search' => true,
'show_in_admin_bar'   => false,
'show_in_nav_menus'   => false,
'publicly_queryable'  => false,
'query_var'           => false
));
0v3rth3d4wn
0v3rth3d4wn
May 24, 2014 19:01 PM

One. From your functions file.

add_action( 'template_redirect', 'redirect_cpt_singular_posts' );
    function redirect_cpt_singular_posts() {
      if ( is_singular('your-cpt-slug') ) {
        wp_redirect( home_url(), 302 );
        exit;
      }
    }

Two. From your single.cpt.php file:

<?php wp_redirect( home_url() ); exit; ?>
Brad Dalton
Brad Dalton
May 24, 2014 19:10 PM

Just setting the argument

'publicly_queryable'  => false

when you call register_post_type()

Gustavo
Gustavo
December 27, 2014 21:38 PM

Working from of Sven's really good answer, I rewrote his function to make it easier to add multiple post types using in_array() in the if statement and then redirecting to the archive page instead of the home page.

(by the way, I think the setting query_var and/or publically_queryable to false will disable not only the single views, but also the native archive view, overriding 'has_archive' => true. In that case you can still set up a custom WP_query and create your own archive page, in a template, but the main query won't do that any more, will it?)

function fq_disable_single_cpt_views() {
  $queried_post_type = get_query_var('post_type');
  $cpts_without_single_views = array( 'my-post-type', 'my-other-post-type' );
  if ( is_single() && in_array( $queried_post_type, $cpts_without_single_views )  ) {
    wp_redirect( home_url( '/' . $queried_post_type . '/' ), 301 );
    exit;
  }
}

add_action( 'template_redirect', 'fq_disable_single_cpt_views' );
slashbob
slashbob
July 09, 2016 16:27 PM

Both of the following need to be false :

'public' => false,
'publicly_queryable' => false,
piyush
piyush
December 07, 2017 11:00 AM

Related Questions


Create sub single pages

Updated March 22, 2017 08:08 AM

Single page not working for custom post type

Updated April 26, 2016 09:03 AM

Edit single page from plugin

Updated October 12, 2016 09:03 AM

How to display single post from custom post type loop?

Updated February 18, 2017 21:08 PM