Skip Navigation

[Resolved] Retrieving child post values to put in an array in PHP

This support ticket is created 6 years, 11 months 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.

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 2 voices.

Last updated by jeffC-3 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#601877

I have a post field group assigned to a post type that then has a parent post type, basically a post type relationship. (https://toolset.com/documentation/user-guides/creating-post-type-relationships/)

I display the custom fields I want in a content template for the output page. A view for the child posts is called within the content template that then displays all the child posts for that post type.

After the page is drawn for the user, I then have a hidden form using Gravity Forms that gets populated with the same data in case a user wants to click an "update" link to then display the form and update any fields on the form that have already been pre-populated.

Hopefully that makes sense. 🙂 If not, here is a page of what I am trying to describe: hidden link
The Vineyard Addresses are the child posts, and then you click update to display the form.

The problem I have is getting the values of those child posts using PHP code in functions.php to then populate the 3 sections of the form that have the Vineyard Addresses info.

I am guessing I need to use some kind of code that is referenced here: https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/

But the code currently being used to get the data to send to the form is fine for "upper level" custom fields by creating an array, but the child post ones is what I am struggling with in order to pass the values in the same array.

Here is the code that is being used to get the data right now:

function populate_fields( $value, $field, $name ) {
$mk = filter_gpm( get_post_meta( get_the_ID() ) );

$values = array(
'gf-title' => $mk['wpcf-title'],
'gf-owners' => $mk['wpcf-owners'],
'gf-contact-address' => $mk['wpcf-contact-address'],
....
);

return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}

function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}

function filter_gpm($array){
$mk = array();
foreach($array as $k => $v){
if(is_array($v) && count($v) == 1){
$mk = array_push_assoc($mk, $k, $v[0]);
} else {
$mk = array_push_assoc($mk, $k, $v);
}
}
return $mk;
}

I appreciate any help someone can provide on how to fill the form array with the values of the child posts.

#601996

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

I can try and help you with this, though I'll need some more details.

I think I follow what's going on.

The page you linked to is a single Vineyard post, which includes a View to display Vineyard Addresses child posts, right?

And you have a Gravity Form, initially hidden, which you want populated with content both from the Vineyard post itself and the child Vineyard Addresses posts, yes?

Looking at your code, the key part is the function populate_fields. How do you use this function? Where is it called from?

Getting the fields of the Vineyard post works, getting the fields of the child Vineyard Addresses fields doesn't, right?

You have an existing solution that partially works, but I suspect rather than trying to modify this to get it to work, a fresh start may be easier, but let me get more details from you first (in particular, how and where you are using the populate_fields function).

#602040

Hi Nigel,
You understand exactly what exists and what I am trying to do.

This is where I got the code that calls the populate_fields function and the inner part of that function:
hidden link
and then:
https://developer.wordpress.org/reference/functions/get_post_meta/#comment-1674

And finally from the vineyard content template:
[gravityform id="5" title="false" description="false" ajax="true"]

You should now have all the code I currently have. Hope that helps!

#602534

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

Firstly, sorry for the delay getting back to you over the holidays.

Thanks for the links.

You are using a single filter to populate all of the dynamic fields of the Gravity Form, but you are only getting the custom field values from the current post (using the function filter_gpm) and using these to set the GF field values.

You aren't retrieving fields from the child posts to set the related GF fields.

So your filter_gpm function needs to also use get_posts to get the child posts of the current post (if the parent post slug is "vineyard" they will have a post meta field _wpcf_belongs_vineyard_id with a value equal to the id of the current post).

Where are the addresses stored? If they are in the post body of the child posts then the array of post objects returned by the previous query will have the data you need, but if the addresses are stored as post meta then for each of the posts you will need to retrieve the corresponding post meta to set the values of the GF fields.

Which of these is it? If you need help with the above, let me know and I should be able to point you in the right direction.

#602573

Thanks Nigel. The addresses are stored in the post meta of the child posts and there are 3 custom fields in the child posts to retrieve. Unfortunately I do need help on how to retrieve these values. Thanks.

#602624

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

I don't know the field names in your GF form or the slugs of the fields on your Vineyard Address post type, or even the post type slug, so I have made some assumptions to produce the following code example.

Hopefully you can follow what is happening and edit accordingly.

Feel free to ask me if you are unsure of something, but please also bear in mind that supplying, debugging and supporting custom code are all outside of our support policy.

function populate_fields( $value, $field, $name ) {

	$mk = filter_gpm( get_post_meta( get_the_ID() ) );

	$values = array(
		'gf-title' => $mk['wpcf-title'],
		'gf-owners' => $mk['wpcf-owners'],
		'gf-contact-address' => $mk['wpcf-contact-address'],
	);

	// get child posts of current post
	$args = array(
		'post_type'		=>	'vineyard-address',
		'post_status'	=>	'publish',
		'numberposts'	=>	-1,
		'meta_key'		=>	'_wpcf_belongs_vineyard_id',
		'meta_value'	=>	get_the_ID()
	);
	$child_posts = get_posts( $args );

	foreach ( $child_posts as $key => $child_post ) {

		// might need n which is $key + 1
		$n = $key + 1;

		// get custom fields for nth child post
		$address_field = get_post_meta( $child_post->ID, 'wpcf-address-field', true );
		$gps_field = get_post_meta( $child_post->ID, 'wpcf-gps-field', true );
		$acres_field = get_post_meta( $child_post->ID, 'wpcf-acres-field', true );

		// update $values with custom field values
		$values[ 'gf-vineyard-address' . $n ] = $address_field;
		$values[ 'gf-vineyard-gps' . $n ] = $gps_field;
		$values[ 'gf-vineyard-acres' . $n ] = $acres_field;
	}

	return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}
#602745

Hi Nigel,
I understand your logic. Give me a couple days to see if it all works with the correct names and such, and I'll let you know. Thanks so much for the help!

#602908

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sure thing, let me know how you get on...

#603566

Hi Nigel,
After changing slug and field names, would you believe the code worked perfectly the first time? Great job! Now it has me thinking how I can utilize the same concept with two other areas. 🙂 Thanks again!