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,
By the way, I'm talking about the normal WordPress child/parent relationship.
Thanks.
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
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") );
}
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
That was it! My issue is resolved now. Thank you!