0

I have custom post types. grandparent, parent and child. With this code I can select the grandparent of the parent in the dashboard.

function add_grandparent_to_parent_dropdown()
{
    add_meta_box('grandparent-parent-meta-box', 'Grandparent', 'grandparent_to_parent_attributes_meta_box', 'parent', 'side', 'high');
}
add_action('add_meta_boxes', 'add_grandparent_to_parent_dropdown');

function grandparent_to_parent_attributes_meta_box($post)
{

    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;       

    $pages = wp_dropdown_pages(
        array(
            'post_type'        => 'grandparent',
            'selected'         => $post->post_parent,
            'name'             => 'parent_id',
            'authors'          => $current_user_id,
            'show_option_none' => __('(no parent)'),
            'sort_column'      => 'menu_order, post_title',
            'echo'             => 0,
        )
    );
    if (!empty($pages)) {
        echo $pages;
    }
}

Now when I go to the child post type and try to select the parent post for that the dropdown menu will not show if the parent post already has a selection for grandparent post.

On the other hand, when no posts have a parent post selected and I go to child I can select a parent there and can then go to parent and select a grandparent post just fine. The dropdown menu will show the right posts.

So what I am wondering is, how can I make wp_dropdown_pages show me a list of parent posts when those parent posts already have a grandparent selected.

This is the code to select the parent of the child post type.

function add_parent_to_child_dropdown()
{
    add_meta_box('parent-child-meta-box', 'Parent', 'parent_to_child_attributes_meta_box', 'child', 'side', 'high');
}
add_action('add_meta_boxes', 'add_parent_to_child_dropdown');

function parent_to_child_attributes_meta_box($post)
{

    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;       

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.