phparrayswordpresscustom-post-type

Wordpress Custom Post Type not allowing Parent/Child pages


Quite stuck on this one. I have a CPT set up and it's working just fine, except for one small issue. The 'Parent' select box does not show up under the 'Page Attributes' meta box. I've checked and rechecks, so I unless I am missing something really obvious, I am at a loss as to why this is happening. Here's the code:

function create_posttype_4() {

    register_post_type( '1:White',
        array(
            'menu_icon' => 'dashicons-book-alt',
            'labels' => array(
                'name' => __( '1:White' ),
                'singular_name' => __( '1:White' )
            ),
            'public' => true,
            'rewrite' => array(
                'slug' => 'white',
                'with_front' => true
            ),
        )
    );
}

add_action( 'init', 'create_posttype_4' );
 
function custom_post_type_4() {

    $labels = array(
        'name'                  => _x( '1:White', 'Post Type General Name', 'thesilent_2024r0' ),
        'singular_name'         => _x( '1:White', 'Post Type Singular Name', 'thesilent_2024r0' ),
        'attributes'            => _x( '1:White', 'Page Attributes', 'thesilent_2024r0' ),
        'menu_name'             => __( '1:White', 'thesilent_2024r0' ),
        'all_items'             => __( 'All 1:White', 'thesilent_2024r0' ),
        'view_item'             => __( 'View 1:White', 'thesilent_2024r0' ),
        'add_new_item'          => __( 'Add New 1:White', 'thesilent_2024r0' ),
        'add_new'               => __( 'Add New 1:White', 'thesilent_2024r0' ),
        'edit_item'             => __( 'Edit 1:White', 'thesilent_2024r0' ),
        'update_item'           => __( 'Update 1:White', 'thesilent_2024r0' ),
        'search_items'          => __( 'Search 1:White', 'thesilent_2024r0' ),
        'not_found'             => __( 'Not Found', 'thesilent_2024r0' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'thesilent_2024r0' ),
        'parent'                => __( '1:White', 'Parent', 'thesilent_2024r0' ),
        'labels'                => $labels,
    );

    $args = array(
        'hierarchical'          => true,
        'capability_type'       => 'page',
        'supports'              => array(
            'title',
            'thumbnail',
            'editor',
            'excerpt',
            'author',
            'comments',
            'post-formats',
            'parent',
            'page-attributes'
        ),      
        'has_archive'           => true,
        'taxonomies'            => true,
        'query_var'             => true,
        'label'                 => __( '1:White', 'thesilent_2024r0' ),
        'description'           => __( '1:White', 'thesilent_2024r0' ),
        'show_ui'               => true,
        'show_in_menu'          => true,
        'show_in_nav_menus'     => true,
        'show_in_admin_bar'     => true,
        'menu_position'         => 1,
        'can_export'            => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'show_in_rest'          => true,
    );  

    register_post_type( '1:White', $args );
}
add_action( 'init', 'custom_post_type_4', 0 );

Solution

  • In your current code, you are registering 2 times the same custom post type "1:White" using 2 separated functions, with different arguments (settings). This should be done only once, merging arguments.

    Also, $labels array of arguments in your 2nd function is wrongly set in its own array, and should be set instead in the main arguments array $args.

    So try to replace your 2 functions with the following unique (merged) one:

    function add_custom_post_type_4() {
        $text_domain = 'thesilent_2024r0';
    
        register_post_type( '1:White', array(
            'can_export'            => true,
            'capability_type'       => 'page',  
            'description'           => __( '1:White', $text_domain ),
            'exclude_from_search'   => false,
            'hierarchical'          => true,  
            'has_archive'           => true,
            'label'                 => __( '1:White', $text_domain ),
            'labels'                => array(
                'name'                  => _x( '1:White', 'Post Type General Name', $text_domain ),
                'singular_name'         => _x( '1:White', 'Post Type Singular Name', $text_domain ),
                'attributes'            => _x( '1:White', 'Page Attributes', $text_domain ),
                'menu_name'             => __( '1:White', $text_domain ),
                'all_items'             => __( 'All 1:White', $text_domain ),
                'view_item'             => __( 'View 1:White', $text_domain ),
                'add_new_item'          => __( 'Add New 1:White', $text_domain ),
                'add_new'               => __( 'Add New 1:White', $text_domain ),
                'edit_item'             => __( 'Edit 1:White', $text_domain ),
                'update_item'           => __( 'Update 1:White', $text_domain ),
                'search_items'          => __( 'Search 1:White', $text_domain ),
                'not_found'             => __( 'Not Found', $text_domain ),
                'not_found_in_trash'    => __( 'Not found in Trash', $text_domain ),
                'parent'                => __( '1:White', 'Parent', $text_domain ),
            ),
            'menu_icon'             => 'dashicons-book-alt',
            'menu_position'         => 1,
            'public'                => true,
            'publicly_queryable'    => true,
            'query_var'             => true,
            'rewrite' => array(
                'slug'                  => 'white',
                'with_front'            => true
            ),
            'show_in_admin_bar'     => true,
            'show_in_rest'          => true,
            'show_in_menu'          => true,
            'show_in_nav_menus'     => true,
            'show_ui'               => true,
            'supports'              => array(
                'title',
                'thumbnail',
                'editor',
                'excerpt',
                'author',
                'comments',
                'post-formats',
                'parent',
                'page-attributes'
            ),
            'taxonomies'            => true,
        ) );
    }
    add_action( 'init', 'add_custom_post_type_4', 0 );
    

    As you will see, now the Parent select box appear and is functional:

    enter image description here

    Don't forget to flush rewrite rules by going on WordPress Settings > Permalinks and click on "Save changes".

    Code goes in functions.php file of your child theme (or in a plugin).