I'm trying to set my custom post type staff
as a child of the page about-us
for example:
Go from example.com/staff/john-doe
to example.com/about-us/john-doe
I've currently tried every possible post on Google and on Stack to no avail, the closet solution I could find is adding the below to my functions.php
, which adds a meta box enabling me to set the parent page manually for individual staff members.
add_action('admin_menu', function() { remove_meta_box('pageparentdiv', 'chapter', 'normal');});
add_action('add_meta_boxes', function() { add_meta_box('chapter-parent', 'Part',
'chapter_attributes_meta_box', 'chapter', 'side', 'high');});
function chapter_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' => 'part', '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.
}
However when combining this with 'rewrite' => array('slug' => 'about-us'),
it seems to cause example.com/about-us/about-us/john-doe
and without it example.com/staff/about-us/john-doe
neither work correctly!
Firstly i'm wondering if there is a solution to my problem above, and secondly is there a better way to achieve this and potentially without having to manually select the parent page for each individual post, but rather an entire custom post type set as a child of a page?
I will be sure to update if I make any progress on this!
Not to confuse you anymore but the motive behind trying to do this is navigation related, so my menu system automatically adjusts. If this wasn't a concern I would simply use rewrite and have a custom menu which I'd need to update every time a page was moved or added! Which obviously is not ideal when you have multiple CPT's.
This might have something to do with your register_post_type() call. Your rewrite might need to look like this:
'rewrite' => array('slug' => 'about-us', 'with_front' => false )
The "with_front" part should remove anything you are currently prepending to any post. For example, if in your permalinks section you are prepending all posts with staff (which it appears you are doing, though it's just a guess as I can't see your setup), with_front would ignore it.
With this solution, you won't need that extra meta box function either.
Don't forget to visit the permalink page after this change to make sure permalinks are flushed and you get no 401s...