Skip Navigation

[Resolved] Displaying/getting child posts from within a function

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to display child post with custom fields of given parent using PHP.

Solution:
To display child post from given parent using PHP we have complete documents in regards to this issue.

Please see the follwoing documentation:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/
[Section: "Displaying child posts using Types PHP functions"]

To get custom field value using PHP you should use get_post_meta() function. You can see the example of that on the following reply.
https://toolset.com/forums/topic/displayinggetting-child-posts-from-within-a-function/#post-390941

Relevant Documentation:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

This support ticket is created 8 years ago. 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.

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 11 replies, has 2 voices.

Last updated by SteveWebStudioNW 8 years ago.

Assisted by: Minesh.

Author
Posts
#390601

I am trying to list custom fields from child post inside a function.

I have setup a custom post type called 'listings' and a child post type called 'items'

I have to use a function for a process and firstly i need to gather all the fields from all the child posts.

I'm firstly trying to display the post_title, (which is using the default wordpress 'post_title') which is not working (code below) it doesnt return anything.

Secondly I need to pull out my custom fields from the child posts 'description','feature1','feature2', but I cant get the page title and I don't know the precise way of requesting the custom fields.

You help would be greatly appreciated!

$pages = get_pages('child_of=8');

	foreach($pages as $page)
	{

		$html = $html. "-".$page->post_title;
	} 

return $html;
#390793

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

We have a document about how you will be able to query and display your child posts.

For example, to load ‘room’ child items::

$child_posts = types_child_posts("room");
foreach ($child_posts as $child_post) {
  echo $child_post->post_title;
  echo $child_post->fields['description'];
}

More info:
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/

I hope this solution will help you to resolve your issue.

#390860

Thanks for the response, I'm trying this in a function (child theme functions.php) yet it doesn't seem to work?

#390870

Minesh
Supporter

Languages: English (English )

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

Just wanted to be more precise, you have parent/child relationship created using types - correct? if yes, I would like to know where and how you would like to display posts with which custom fields?

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#390872

maybe it's because I am not specifying a parent post id? how would you do this with this code?

$child_posts = types_child_posts("room");
foreach ($child_posts as $child_post) {
echo $child_post->post_title;
echo $child_post->fields['description'];
}

#390877

Minesh
Supporter

Languages: English (English )

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

I think you miss the document part.

This example returns all rooms that belong to the current property and will sort them by the description field.

$childargs = array(
'post_type' => 'items',
'numberposts' => -1,
'meta_query' => array(array('key' => '_wpcf_belongs_property_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);

So, here "_wpcf_belongs_property_id" is your parent. You should replace "propery" with your parent post slug. For example: _wpcf_belongs_listings_id

#390901

The parent post type is called 'ebay-listing' the child posts i need to display is called 'ebay-item'

I dont have a 'current property' as such in my code I need to specify a specific 'ebay-listing' post id, in this case post id=8, and display all the child posts of this.

This is my code now (sorry my php experience is patchy at best)

$childargs = array(
'post_type' => 'ebay-item',
'numberposts' => -1,
'meta_query' => array(array('key' => '_wpcf_belongs_property_id', '8' => get_the_ID()))
);
$child_posts = get_posts($childargs);

foreach ($child_posts as $child_post) {
echo $child_post->post_title;
echo $child_post->fields['description'];
}

#390902

Minesh
Supporter

Languages: English (English )

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

To guide you in right direction I would like to check your setup.

*** 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 would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

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

#390914

Minesh
Supporter

Languages: English (English )

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

Unfortunately, wp-admin access details are not working for me.

Could you please resend it to me. Also, could you please send me problem URL?

#390924

Problem url (my test file) hidden link

just reset user password and tested, now works, sorry!

#390941

Minesh
Supporter

Languages: English (English )

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

Could you please check your template file. I've done necessary amendments.

You can get your custom fields values for example for feature1 as follows:

echo $feature1 = get_post_meta($child_post->ID,'wpcf-feature1',true);

Take example of above code and replace your custom field name to get values of other fields. For example:

echo $feature2 = get_post_meta($child_post->ID,'wpcf-feature2',true);

I hope this solution will help you to resolve your issue.

#390960

Many many thanks Minesh!

Your a superstar, just when I was about to throw my PC out the window:)

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