最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Displaying different custom post types under one admin menu

programmeradmin1浏览0评论

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

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

Share Improve this question edited Nov 17, 2014 at 11:02 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Nov 17, 2014 at 8:25 DongsanDongsan 335 bronze badges 4
  • 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 Commented Nov 17, 2014 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 Commented Nov 17, 2014 at 11:26
  • what is more important, users should be given different capabilities per each custom post type...so the thing is complicated... – Dongsan Commented Nov 18, 2014 at 0:40
  • Hi, I hope this can help you [parent-child custom-post-type][1] [1]: wordpress.stackexchange/questions/181134/… – Harry Commented Sep 13, 2017 at 6:47
Add a comment  | 

1 Answer 1

Reset to default 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]
发布评论

评论列表(0)

  1. 暂无评论