Templates for hierarchical custom post type

I'm a bit confused with templates for custom post types defined as hierarchical. I've got a CPT set to hierarchical, not strictly in order to have a nested hierarchy, but so that they can be ordered by the Simple Page Ordering plugin. Here's the code:

register_post_type(
    'case_study', array(
        'label'                 = _x( 'case studies', 'post type plural name' ),
        'labels'                = array(
            'name'                  = _x( 'Case studies', 'post type general name' ),
            'singular_name'         = _x( 'Case study', 'post type singular name' ),
            'menu_name'             = _x( 'Case studies', 'admin menu' ),
            'name_admin_bar'        = _x( 'Case study', 'add new on admin bar' ),
            'add_new'               = _x( 'Add New', 'case study' ),
            'add_new_item'          = __( 'Add New Case study' ),
            'new_item'              = __( 'New Case study' ),
            'edit_item'             = __( 'Edit Case study' ),
            'view_item'             = __( 'View Case study' ),
            'all_items'             = __( 'All Case studies' ),
            'search_items'          = __( 'Search Case studies' ),
            'parent_item_colon'     = __( 'Parent Case studies:' ),
            'not_found'             = __( 'No Case studies found.' ),
            'not_found_in_trash'    = __( 'No Case studies found in Trash.' )
        ),
        'public'                = true,
        'publicly_queryable'    = true,
        'show_ui'               = true,
        'show_in_nav_menus'     = true,
        'show_in_menu'          = true,
        'show_in_admin_bar'     = true,
        'menu_position'         = 20, // Below Pages
        'menu_icon'             = 'dashicons-clipboard', // @link https://developer.wordpress.org/resource/dashicons/
        'query_var'             = false,
        'rewrite'               = array( 'slug' = 'case-study', 'with_front' = false ),
        'capability_type'       = 'case_study',
        'map_meta_cap'          = false,
        'capabilities' = array(
            'publish_posts'         = 'publish_case_studies',
            'edit_posts'            = 'edit_case_studies',
            'edit_others_posts'     = 'edit_others_case_studies',
            'delete_posts'          = 'delete_case_studies',
            'delete_others_posts'   = 'delete_others_case_studies',
            'read_private_posts'    = 'read_private_case_studies',
            'edit_post'             = 'edit_case_study',
            'delete_post'           = 'delete_case_study',
            'read_post'             = 'read_case_study',
        ),
        'has_archive'           = false,
        'hierarchical'          = true, // Set to true to allow ordering
        'supports'              = array( 'title', 'editor', 'custom-fields', 'thumbnail', 'revisions', 'pilau-author' ),
        'taxonomies'            = array( 'topic' ),
    )
);

By default a hierarchical CPT uses the page.php template instead of single.php - which took me a while to realise, but it makes sense.

However, now I want to create a template specifically for this CPT, and single-case_study.php doesn't work. Neither does page-case_study.php.

How do you create a template specifically for a hierarchical CPT?

Topic template-hierarchy hierarchical templates custom-post-types Wordpress

Category Web


Old question but maybe it helps someone else:

You could do something like this in page.php:

$postType = get_post_type();

if($postType == 'case_study'){
   include('single-case_study.php');
} else{
   // Do your page.php stuff

I sense I'm still missing something, but for now I've approached this another way. Since the only reason for the CPT to have hierarchical set to true is so that Simple Page Ordering will work (I'm not actually after a nested hierarchy), I went back to the plugin's FAQ and found that it is possible to order non-hierarchical post types. Either include 'page-attributes' in the supports array or use the simple_page_ordering_is_sortable filter, then you can click 'Sort by Order' on the posts admin listing page and drag-and-drop.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.