Skip Navigation

[Resolved] How to get grandchildren of a custom post types made by Types plugin?

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Beda 7 years, 11 months ago.

Assisted by: Beda.

Author
Posts
#400348

Hello everyone!

I'm trying to get a list of children and grand children of a custom post type I made with Types plugin. I have 3 custom post types: Serie, Saison, Episode. Serie is parent of Saison, and Saison is parent of Episode. And I want to display Saison and Episode on the parent page like that.
Saison 1
---Episode 1
---Episode 2
---Episode 3
---............
Saison 2
---Episode 1
---Episode 2
---.............

I tried this code I found on this support forum

$saison_child_posts = types_child_posts('saison');
    foreach ($saison_child_posts as $saison_child_post) {
    $saison_parent_id = wpcf_pr_post_get_belongs($saison_child_post->ID, 'get_post_type( $post )');
    $saisons = get_post($saison_parent_id);
        foreach ($saisons as $saison) {
            $episode_child_posts = types_child_posts('episode');
                foreach ($episode_child_posts as $episode_child_post) {
  $episode_parent_id = wpcf_pr_post_get_belongs($episode_child_post->ID, $saison->post_type);
           
                $episodes = get_post($episode_parent_id);
            }
        }
    }

But I don't get anything.

Before I used the code below to get the child posts but I don't know how to get the grand children.

$args = array( 'post_type' => 'saison', 'numberposts' => -1, 'orderby' => 'meta_value','meta_query' => array(array('key' => '_wpcf_belongs_serie_id', 'value' => get_the_ID())) );
				 $loop = new WP_Query( $args ); while ( $loop->have_posts() ): $loop->the_post(); ?>

I hope you can help me.
Thanks!!!

#400572

1. types_child_posts('saison'); will not return child of Saison, but the posts that are child of whatever, of Type saison.

2. So if you have Parent - Child One - Child Two, the call to get Child One while on a Parent post, is:
types_child_posts('child-one');
https://toolset.com/documentation/user-guides/many-to-many-post-relationship/ > page's bottom

3. wpcf_pr_post_get_belongs( $post_id, $post_type ); is not documented as far I know, I requested a API update for this, but it should get the Child Posts of a given Parent ID/ Post type.

Anyway, since you want to display GranChild of a given Grandparent, I suggest:

1. Get_posts (parent Type)
2. get_posts for the Child of Parents
3. get_posts for the child of child of parents (grandchild)
https://codex.wordpress.org/Template_Tags/get_posts

You can each get the parent post ID by checking against the hidden field _wpcf_belongs_parent-post-type-slug_id

You can also use the WordPress API for this:
get_post_meta()
https://developer.wordpress.org/reference/functions/get_post_meta/

I rather recommend To use Views Plugin, which will enable you to display this grand child posts with 3 Views, in about 20 minutes you would be all set.
You can read more about this here:
https://toolset.com/documentation/user-guides/views/
https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

#400653

Thanks for your help,
I tried to use get_posts but I still don't get the grandchild posts. I am a beginner in PHP code so that's not easy. I tried this code:

<div>	 <?php
        $args = array(
            'post_type' => 'saison',
            'posts_per_page' => -1,
             'orderby' => 'meta_value',
             'meta_query' => array(array('key' => '_wpcf_belongs_serie_id', 'value' => get_the_ID())) );
        $child_level = get_posts($args);?>
 <a href="<?php the_permalink($child_level ); ?>" title="Retour page série"><?php echo $child_title= get_the_title($child_level ) ?></a>

    <?php  if($child_level) ;
            foreach($child_level as $child_page) {
                $chgs = array(
                    'post_type' => 'episode',
                    'orderby' => 'title',
                    'posts_per_page' => -1,
                     'post_parent' => $child_page->ID);};
                    
            $grandchild_level = get_posts( $chgs );?>
        <a href="<?php the_permalink($grandchild_level ); ?>" title="Retour page série"><?php echo $grandchild_title= get_the_title($grandchild_level ) ?></a>

            </div>

I only get one child post and it doesn't display any grandchild posts. I don't know how to use the meta key _wpcf_belongs_parent-post-type-slug_id in this case.

#400748
post_parent

Is not getting Types relationships but WordPress native parent posts as in pages.

_wpcf_belongs_parent-post-type-slug_id

This Field will hold (in the Child post) the ID of the Parent Post.

So if you have a certain parent Post, you can get all Children Posts that have the ID stored in that Field.

Then, you can query again and check again the Child of the Child's ID, for it's parent Post ID stored in it's hidden field.

This is custom code. If you are not familiar with PHP I strongly suggest to use Views.

We can not assist Custom Code in the Free Types Forum or in our Paid forums, we do however assist Toolset Features and give instructions and examples on code so you can build on it, but we do not provide ready to go Custom Code.
https://toolset.com/toolset-support-policy/

If you need help with custom programming work which is beyond the scope of our support, I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

DOC:
https://codex.wordpress.org/Template_Tags/get_posts

Example on how to get Posts that are Child of a given Post with ID defined in $postID

$postID = //Define the Post ID of the Post where you use this code on (current post, post in loop...)
$args = array(
	'posts_per_page'   => -1,
	'orderby'          => 'date',
	'order'            => 'DESC',
	'meta_key'         => '_wpcf_belongs_post-type-slug_id',
	'meta_value'       => $postID,
	'post_type'        => 'post-type-slug',
	'post_status'      => 'publish',
	'suppress_filters' => true 
);
$posts_array = get_posts( $args ); 

//Now we have and array of ALL posts of type post-type-slug that are Types Child of the Post postID

Now you can go ahead, foreach over above Array, and repeat.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.