To disable the creation of pages for a custom post type, but still allow a page with the post type’s slug to be created, you can use the following method:

  1. Use the register_post_type function to register your custom post type, and set the public parameter to false. This will prevent pages of that post type from being created.
register_post_type( 'my_custom_post_type', array(
    'public' => false,
    // other post type args
) );
  1. Create a new page template that is used to display the custom post type’s slug. This template should use the query_posts function to retrieve the custom post type and display it on the page.
<?php
/*
Template Name: Custom Post Type Page
*/
query_posts( array( 'post_type' => 'my_custom_post_type' ) );
// The loop to display the post type
?>
  1. Use the template_include filter to redirect the custom post type’s slug to the new page template. This filter is used to change the template that is used to display a specific page.
function my_custom_post_type_template( $template ) {
    if ( is_singular( 'my_custom_post_type' ) ) {
        $new_template = locate_template( 'custom-post-type-page.php' );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }
    return $template;
}
add_filter( 'template_include', 'my_custom_post_type_template', 99 );

This method allows you to disable the creation of pages for a custom post type, while still allowing a page with the post type’s slug to be created, and displaying the custom post type on that page. It’s important to note that this method will only work when the custom post type’s permalink structure is set to ‘Post Name’ in the permalinks settings, otherwise it will not work properly.

(Visited 26 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window