Skip Navigation

[Resolved] How to call custom field from parent post

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by gabrielB 5 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1245792

Hello again.

I have parent/child posts and I'm trying to call data from the parent posts' custom field into the child post.

I need some php, please. This is what I have so far and it isn't working.

'
$args = array(
'post_parent' => get_the_ID(), // Current post's ID
);

$image_url = types_render_field("banner-image", array("output"=>"raw", "post_id"=>$args) );
'

Thanks,

#1245872

By the way, I'm talking about the normal WordPress child/parent relationship.

Thanks.

#1245875

Hi Gabriel,

Thank you for contacting us and I'll be happy to assist.

As explained in the documentation of the "Type Fields API" ( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ ), it supports the "item" attribute ( and not the "post_id" ), for the case when the data from a post different than the current one is needed.

Example:


$image_url = types_render_field("banner-image", array("output"=>"raw", "item"=>$parent ) );

You'll find the details around how to use this "item" attribute, to call the values from the child/parent posts, in this guide:
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1245877

Hi Waqar,

thanks for this but it didn't seem to work. Here's my full code:


        if ( $post->post_parent > 0 ) {
            // This is a subpage
            $image_url = types_render_field("banner-image", array("output"=>"raw", "item"=>$parent ) );


        } else {
            // This is not a subpage
            $image_url = types_render_field("banner-image", array("output"=>"raw") );
        }

#1246244

Hi Gabriel,

To troubleshoot this, I'll suggest first debugging if the global $post is bringing in the correct current post's data, where you're using the custom PHP function.
( ref: https://codex.wordpress.org/Function_Reference/$post )

I would echo/print the values for the $post, $post->ID (current post's ID) and $post->post_parent (current post's parent ID) to see if the correct values are available in the custom code.

Once it is confirmed that the correct IDs are available, you can use the values in the "item" attribute:


// parent post
$image_url = types_render_field("banner-image", array("output"=>"raw", "item"=>$post->post_parent ) );

// current post
$image_url = types_render_field("banner-image", array("output"=>"raw", "item"=>$post->ID ) );

regards,
Waqar

#1247857

That was it! My issue is resolved now. Thank you!