How to add a regular page under a custom post type?

I'm building a WordPress website that should have the following structure:

  • Project A (project post type)
    • About (regular page)
    • Location (regular page)
    • Partners (regular page)
    • News (regular page)
  • Project B (project post type)
    • About (regular page)
    • Book (regular page)

As you see, each project can have random pages below it. The admin should be able to add any page as he wants.

I found some solutions based on setting the hierarchical attribute from my custom post type to true and making it supports page-attributes. Those don't fit my needs because it'll only make possible to add a project under another project.

There is a way to do what I need?

Topic hierarchical pages custom-post-types Wordpress

Category Web


I think you're looking for something like this (from Post parent of different type):

I was thrilled to find out what I could do with WordPress Custom Post Types, but very much underwhelmed that you weren’t able to set the parent of a post to be a post of a different type. That is, say you’ve created a custom post type “Chapter”, you couldn’t set the parent of a “Chapter” post to be a “Part” post. The parent had to be of the same type.

I’ve found a simple workaround which essentially replaces the “restriction” WP puts on the parent type. Besides the meta box on the edit post page, there are actually no restrictions on what post type the parent may be.

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
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.