Displaying different custom post types under one admin menu

I am creating multiple custom post types that have hierarchies, and a few of them are strongly related like a parent-child.

For example, a house custom post type has bedroom and kitchen custom post types as its children.

What I’m trying to do is to display these three custom post types under one admin ui menu so that it may display like below:

House (<– this shall be a menu item in admin ui)

House A (<– this is a post that belongs to ‘house’ custom post type )

-Bedroom 1 (<– this is a post that belongs to ‘bedroom’ custom post type )

-Bedroom 2

-Kitchen 1 (<– this is a post that belongs to ‘kitchen’ custom post type )

-Kitchen 2

-Kitchen 3

House B

-Bedroom 3

-Bedroom 4

-Kitchen 4

So far I could make these custom post types with hierarchy attribute, but couldn’t display them altogether in one admin ui menu. I can display the child custom post types under the parent custom post type by using 'show_in_menu' => 'edit.php?post_type=house', but that is not what I want.

Anyone has idea about how to display different custom post types with parent-child relationship together in one admin menu?

I know there are plugins for this, but I need to do this programmatically.

ADDED

To make my question more clear, this is what I’m trying to achieve but failing:

$args = array(
    'post_title'    => 'House as a parent post',
    'post_type'     => 'house',
    'post_status'   => 'publish'
);

$pid = wp_insert_post( $args );
do_action( 'wp_insert_post', 'wp_insert_post' );

$args = array(
    'post_title'    => 'Bedroom as a child post',
    'post_type'     => 'bedroom',
    'post_parent'   => $pid,
    'post_status'   => 'publish'
);

wp_insert_post( $args );
do_action( 'wp_insert_post', 'wp_insert_post' );

What I want is to create two posts from different custom post types to make a parent-child relationship and be displayed in the same admin-menu like under ‘Dashboard > House’.

1 Answer
1

step 1: paste below code in your function.php (change the post_type)

function house_a() {
    $args = array(
        'post_type' => 'house_a',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $returnvariable = "";
        while ($my_query->have_posts()) : $my_query->the_post(); 
            $returnvariabla .= get_the_content();
        endwhile;
        return $returnvariable;
    }
    wp_reset_query();
}
add_shortcode("house_a","house_a");

function house_b() {
    $args = array(
        'post_type' => 'house_a',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $returnvariable = "";
        while ($my_query->have_posts()) : $my_query->the_post(); 
            $returnvariabla .= get_the_content();
        endwhile;
        return $returnvariable;
    }
    wp_reset_query();
}
add_shortcode("house_b","house_b");

step 2: paste the below shortcode in your display page

[house_a]
[house_b]

Leave a Comment