Skip Navigation

[Gelöst] get all the varies post type children post of a parent based on parent id

This support ticket is created vor 2 Jahre, 8 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 Antworten, has 2 Stimmen.

Last updated by davidZ-4 vor 2 Jahre, 8 Monate.

Assisted by: Minesh.

Author
Artikel
#2127479

Tell us what you are trying to do? get a list of all the children post of a parent. the children are various custom type posts

Is there any documentation that you are following?
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Is there a similar example that we can see?
https://toolset.com/forums/topic/retrieving-multiple-child-post-ids/#post-1079482
https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/

What is the link to your site?
hidden link

a 'Plan" have multiple children from a few CPT's
e.g. a plan is a parent of budget, net worth and other custom post types

as far as I understand the current toolset_get_related_posts require a relationship to be declared. i understand that only one relationship can be called out.

is there a way to get all the children of the parent across multiple relationships
i.e plan_budget and plan_net and plan_target etc..

Thanks,
David

#2127695

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Using toolset_get_related_posts() post relationship API function you can only get the one relationship posts.

You should check the example given with the following link using the WP_Query where you can define multiple relaitonship:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#advanced-example

Please note that with 'relationship' you will require to use your relationship slug for example 'plan_budget ' instead of array.

#2128033

Hi Minesh,
Thanks for the quick response.
I still need some help.

I have the following code:

 
	//parent post id	
	$pid = '123456';
	
	// get an child posts of parent
			$query = new WP_Query( 
				array(
					'post_type' => 'plan',
					'posts_per_page' => -1,
					'toolset_relationships' => array(
						array(
							'role' => 'child',
							'related_to' => $pid,
							'relationship' => 'plan-v2-assumption'
						),
						array(
							'role' => 'child',
							'related_to' => $pid,
							'relationship' => 'plan-v2-budget'
						)
					),
				)
			);

	//display the child post array - delete in final function
	$string=implode(",",$query);
	echo $string;

The code return no results
what am I missing?

#2129423

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

The query should return the result object.

If you can share admin access details and tell me where you added the code I'm happy to review it.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2133657

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked and I found that you are using the one-to-one post relationship.

So, rather making things complicated, you can use the Toolset post relationship API function: toolset_get_related_post() to get the related child post for every relationship you have.

For example:

$related_assumption = toolset_get_related_post( $pid,'plan-v2-assumption','child');
$related_budget = toolset_get_related_post( $pid,'plan-v2-budget','child');

The variable $related_assumption and $related_budget will hold the child post ID. You should get the post based on the found ID and display the content as required.

The different relationship example will work when you have two different parents assigned to same child post type:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#advanced-example
[ Consider post type B and C set as parent to post type A (child) and you want to find all A (child) post type posts belongs to different parent B and C.]

In your case its different. You have assumption and budget set as child. That is why we will have to use toolset_get_related_post() or toolset_get_related_posts().

#2138261

My issue is resolved now. Thank you!

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