Skip to content

Instantly share code, notes, and snippets.

@jwondrusch
Last active September 28, 2016 22:55
Show Gist options
  • Save jwondrusch/5807557 to your computer and use it in GitHub Desktop.
Save jwondrusch/5807557 to your computer and use it in GitHub Desktop.
Allow a WordPress custom post type to select another post type as a parent.
// Source: http://janina.tumblr.com/post/3588081423/post-parent-different-type
// Modified to be more generic
add_action('admin_menu', function() {
remove_meta_box( 'pageparentdiv', 'post-type', 'normal');
});
add_action('add_meta_boxes', function() {
add_meta_box( 'post-type-parent', 'Section', 'post_type_attributes_meta_box', 'post-type', 'side', 'high');
});
function post_type_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' => 'post-type', '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.
}
@loorlab
Copy link

loorlab commented Sep 28, 2016

Hi jwondrusch , not working url from CPT Child.

site.com/parent/child --> 404 .

you had that problem? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment