I have just set up a post/parent relationship between a post type “episodes” and a post type “cartoon-series.”
I used this bit of code to add in the meta box to assign the parent from another post type:
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.
}
That worked on the admin screen in allowing me to set the series as a parent to the episode, but when I try to view the post, I get a 404. The url structure is:
domain/episodes/series-name/episode-name
The url for the series is:
domain/cartoon-series/series-name
I’d like the url for the episode to be:
domain/cartoon-series/series-name/episode-name
What am I missing? Is it possible to make an entire post type the child of another post type? So, then I could even get the url for the episodes list to be:
domain/cartoon-series/series-name/episodes
Thanks!
Matt
As requested, here is the code for the two custom post types in question:
$labels = array(
"name" => "Cartoon Series",
"singular_name" => "Cartoon Series",
"menu_name" => "Cartoon Series",
"all_items" => "All Cartoon Series",
"add_new" => "Add New",
"add_new_item" => "Add New Cartoon Series",
"edit" => "Edit",
"edit_item" => "Edit Cartoon Series",
"new_item" => "New Cartoon Series",
"view" => "View",
"view_item" => "View Cartoon Series",
"search_items" => "Search Cartoon Series",
"not_found" => "No Cartoon Series Found",
"not_found_in_trash" => "No Cartoon Series Found in Trash",
"parent" => "Parent Cartoon Series",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "cartoon-series", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" ), );
register_post_type( "cartoon-series", $args );
$labels = array(
"name" => "Episodes",
"singular_name" => "Episode",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "episodes", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" ), );
register_post_type( "episodes", $args );
I’m using the CPT UI plugin, so I can’t edit that code directly. That is just the export code CPT UI provides.
I don’t have any other code that links the two CPTs. Maybe that’s what I’m missing. I just found that code online that places the metabox on the page to do the linking. Is it not enough to do the job? Looks like it sets the post_parent.
Thanks!
Matt