-1

I am trying to connect two custom post types. Through lots of googling, I found the following code that adds a meta box to the admin screen to select a post from another post type as the parent, but how do I set that post as the parent in the database?

add_action('admin_menu', function() {
    remove_meta_box('pageparentdiv', 'episodes', 'normal');
});
add_action('add_meta_boxes', function() {
    add_meta_box('episodes-parent', 'Cartoon Series', 'episodes_attributes_meta_box', 'episodes', 'side', 'default');
});

function episodes_attributes_meta_box($post) {
    $post_type_object = get_post_type_object($post->post_type);
    if ( $post_type_object->hierarchical ) {
        $pages = wp_dropdown_pages(array('post_type' => 'cartoon-series', '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;
        } // end empty pages check
    } // end hierarchical check.
}

Thanks!

4
  • Why would you need the parent - child relationship in the database ? Mar 14, 2015 at 18:16
  • According to another post I had on here, I was not setting the parent anywhere. I assumed he meant I needed to save the parent in the database. I have the metabox to select a parent. Now how do I preserve that setting?
    – Mattaton
    Mar 14, 2015 at 20:29
  • Nevermind, I see on my other thread that the guy says he was mistaken and I was doing it correctly already. I'm just getting 404 errors. See here: wordpress.stackexchange.com/questions/181134/…
    – Mattaton
    Mar 14, 2015 at 20:42
  • This problem was solved under a broader topic here: wordpress.stackexchange.com/questions/181134/…
    – Mattaton
    Mar 14, 2015 at 21:27

1 Answer 1

0

You should use the save_post hook to handle your meta box and set the post_parent field of the post.

An alternative aproch is to look at the page edit page and check what is the name of the "parent page" drop down and use the same for your dropdown instead of the parent_id you use right now.

3
  • As far as I can tell, parent_id is the name of the dropdown on a typical parent page selector. I had this set up before, but I was getting 404 errors on the page. See this thread: wordpress.stackexchange.com/questions/181134/… Oh, I see you already have! :-)
    – Mattaton
    Mar 14, 2015 at 20:35
  • If it doesn't work with parent_id then you need to handle it explicitly in the hook. the other question is not realy related to this one, you can create whatever connection you want between the posts, you don't have to use the wordpress way if you don't gain anything from it (but I assume you can gain, so you are not wasting your time on this IMHO) Mar 14, 2015 at 21:24
  • The problem was resolved in my other, similar thread.
    – Mattaton
    Mar 14, 2015 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.