How to Assign a Parent Page to a Custom Post Type
Sep 14, 2021 | WordPress
//Enter the ID of the parent page
define( 'TSF_PARENT_ID', 'XXX' );
add_action( 'wp_insert_post_data', 'dbs_cpt_parent_page', '99', 2 );
function tsf_cpt_parent( $data, $postarr ) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return $data;
}
//Enter the cpt
if ( $post->post_type == "enter-cpt" ){
$data['post_parent'] = TSF_PARENT_ID ;
}
return $data;
}
//Important settings for the $args array
'rewrite' => array('slug' => 'enter-slug', 'with_front' => false),
'has_archive' => false,
'hierarchical' => false,
'capability_type' => 'page'
Assign a parent page to a custom post type. Helps with breadcrumbs.