Cannot create parent > child > grandchild relationship on custom post types

625 views Asked by At

I have three custom post types:

Pledges (Parent, top level)

Articles (Child, belongs to Pledges)

Sections (Grandchild, belongs to Articles and in turn Pledges)

I can create the three custom post types. Pledges can be created, Articles can be created and assigned a parent Pledge, Sections can be created but I cannot get Articles to show up in the custom meta box to assign one as it’s parent.

It’s all wrapped in a single plugin at the moment which is below.

Any pointers in the right direction would be hugely appreciated – I'm flummoxed.

//Register Cutom Post Types

function custom_post_types(){


    // C R E A T E   P L E D G E S

    $labels = array(
        'name' => 'Pledges',
        'singular_name' => 'Pledge',
        'menu_name' => 'Pledges',
        'admin_menu_bar' => 'Pledges',
        'add_new' => 'Add New Pledge',
        'add_new_item' => 'Add New Pledge',
        'view_item' => 'View Pledge',
        'view_items' => 'View Pledges'
    );

    $args = array(
        'label' => 'Pledges',
        'description' => 'Anti Corruption Pledges',
        'labels' => $labels,
        'menu_icon' => 'dashicons-awards',
        'supports' => array(),
        'taxonomies' => array('pledge'),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'pledges',
        'rewrite' => array('slug' => 'pledges')
    );

    register_post_type('pledge', $args);


    // C R E A T E   A R T I C L E S

    $labels = array(
        'name' => 'Articles',
        'singular_name' => 'Article',
        'menu_name' => 'Articles',
        'admin_menu_bar' => 'Articles',
        'parent_item_colon' => 'Pledge:',
        'add_new' => 'Add New Article',
        'add_new_item' => 'Add New Article',
        'view_item' => 'View Article',
        'view_items' => 'View Articles'
    );

    $args = array(
        'label' => 'Articles',
        'description' => 'A Pledge\'s article',
        'labels' => $labels,
        'menu_icon' => 'dashicons-media-text',
        'supports' => array(),
        'taxonomies' => array('category'),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu'=> true,
        'menu_position' => 6,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'articles',
        'rewrite' => array('slug' => 'articles')
    );

    register_post_type('article', $args);


    // C R E A T E   S E C T I O N S

    $labels = array(
        'name' => 'Sections',
        'singular_name' => 'Section',
        'menu_name' => 'Sections',
        'admin_menu_bar' => 'Sections',
        'parent_item_colon' => 'Article:',
        'add_new' => 'Add New Section',
        'add_new_item' => 'Add New Section',
        'view_item' => 'View Section',
        'view_items' => 'View Sections'
    );

    $args = array(
        'label' => 'Sections',
        'description' => 'An Articles\' section',
        'labels' => $labels,
        'menu_icon' => 'dashicons-format-aside',
        'supports' => array(),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu'=> true,
        'menu_position' => 7,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'sections',
        'rewrite' => array('slug' => 'sections')
    );

    register_post_type('section', $args);
}

add_action('init', 'custom_post_types', 0);



//Create meta boxes for relationships between Pledges and Articles and Sections

//Step One
add_action('admin_menu', function(){
    remove_meta_box('pageparentdiv', array('article', 'section'), 'normal');    
});

//Step Two
add_action('add_meta_boxes', function(){
    add_meta_box('pledge_article-parent', 'Parent content', 'pledge_article_attributes_meta_box', array('article', 'section'), 'side', 'high');
});

//Step Three
function pledge_article_attributes_meta_box($post) {    
    $post_type_object = get_post_type_object($post->post_type);

    if ($post->post_type == 'article') {
        $parent = 'pledge';
    } else {
        $parent = 'article';        
    }
    echo $parent;

    if ( $post_type_object->hierarchical ) {
            $pages = wp_dropdown_pages(array(
                    'post_type'     => $parent, 
                    'selected'      => $post->post_parent, 
                    'name'          => 'parent_id', 
                    'show_option_none'  => __('(no parent)'), 
                    'sort_column'       => 'menu_order, post_title', 
                    'echo'          => 0
        ));

            if ( ! empty($pages) ) {
                echo $pages;
            } 
        }
};
2

There are 2 answers

7
anmari On

Create a pledge to be the parent.

I threw debugs into your code at every step, and saw that wp_dropdown_pages was coming back empty. Realised I had no pledges. Created a pledge. Dropdown appeared.

0
matt_goodall On

This issue has been resolved by adding 'hierarchical' => false to the list of arguments passed to wp_dropdown_pages.

As the documentation states, some parameters for get_pages() can work when creating the dropdown, but it does seem a little counter-intuitive to be setting hierarchical to false when you want to achieve the opposite.