Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialallcleardigital
Courses Plus Student 8,562 PointsPages as parents of Custom Post Types
Hi all,
I have currently used code to assign my CPTs a parent page. After this is done it all works fine, a meta box appears in the edit page and it allows me to enter a page id that I wish to be the CPTs parent.
However, after I go forwards with this my CPT page pulls up a 404 error.
I have tried the following:
1.Saving permalinks to flush permalinks. 2.Done on clean, local wordpress install with no plugins 3.Used manual PHP flush under register post
Here is the code I use to create the page parent relationship,
//Add the meta box callback function
function admin_init(){
add_meta_box("case_study_parent_id", "Case Study Parent ID", "set_case_study_parent_id", "casestudy", "normal", "low");
}
add_action("admin_init", "admin_init");
//Meta box for setting the parent ID
function set_case_study_parent_id() {
global $post;
$custom = get_post_custom($post->ID);
$parent_id = $custom['parent_id'][0];
?>
<p>Please specify the ID of the page or post to be a parent to this Case Study.</p>
<p>Leave blank for no heirarchy. Case studies will appear from the server root with no assocaited parent page or post.</p>
<input type="text" id="parent_id" name="parent_id" value="<?php echo $post->post_parent; ?>" />
<?php
// create a custom nonce for submit verification later
echo '<input type="hidden" name="parent_id_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
}
// Save the meta data
function save_case_study_parent_id($post_id) {
global $post;
// make sure data came from our meta box
if (!wp_verify_nonce($_POST['parent_id_noncename'],__FILE__)) return $post_id;
if(isset($_POST['parent_id']) && ($_POST['post_type'] == "casestudy")) {
$data = $_POST['parent_id'];
update_post_meta($post_id, 'parent_id', $data);
}
}
add_action("save_post", "save_case_study_parent_id");'
Here is the CPT php code:
And here is my cpt php code:
function register_custom_post_case_study() {
$labels = array(
'name' => _x('Case Studies', 'post type general name'),
'singular_name' => _x('Case Study', 'post type singular name'),
'add_new' => _x('Add New', 'Case Study'),
'add_new_item' => __('Add New Case Study'),
'edit_item' => __('Edit Case Study'),
'new_item' => __('New Case Study'),
'view_item' => __('View Case Study'),
'search_items' => __('Search Case Studies'),
'not_found' => __('No Case Studies found'),
'not_found_in_trash' => __('No Case Studies found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => '', 'with_front' => true),
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','author','excerpt','page-attributes')
);
register_post_type( 'casestudy' , $args );
}
add_action('init', 'register_custom_post_case_study');
I cant figure out whats wrong? I have tried manual flush in php under the register post type but to no success. Prior to applying the new URL structure the custom post will show.
Its driving me mad!
Thanks for any help!