2

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'.

  • Why are you using custom post types to achieve this. Create one custom post type with a custom taxonomy and then terms below that that you can "tag" your posts with – Pieter Goosen Nov 17 '14 at 10:59
  • Thank you Pieter Goosen for the suggestion, but I need to give each of these custom post types different meta fields. I dont think taxonomies like category can be given meta fields, at least with the class i am using (wpalchemy)... – Dongsan Nov 17 '14 at 11:26
  • what is more important, users should be given different capabilities per each custom post type...so the thing is complicated... – Dongsan Nov 18 '14 at 0:40
  • Hi, I hope this can help you [parent-child custom-post-type][1] [1]: wordpress.stackexchange.com/questions/181134/… – Harry Sep 13 '17 at 6:47
0

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]
  • Thank you user63570 for the suggestion, but I'm afraid what you suggested is how to display the parent-child relationship in front-end? What I'm in want of is how to display it in admin-ui. – Dongsan Nov 17 '14 at 10:04
  • 1
    Please properly format your code. Highlight it in editor and click on the {} sign to add your code in a code block. As it stands, your code is an unreadable mess. Please file an edit. Thank you – Pieter Goosen Nov 17 '14 at 11:09
  • Got it, I will try to follow the rule. – Dongsan Nov 17 '14 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.