I am trying to: check if parent entry has child elemets, if yes get custom wp types field "termin-von" and conditionally output a text like (request now) if the "termin-von" date is after today.
I visited this URL:
- https://toolset.com/forums/topic/how-to-display-the-child-value-in-a-parent-list-view/
- https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/
I expected to see: get custom field entry from child element
Instead, I got: nothing
- i have added the custom shortcode [child-exists]
- functions.php snippet:
// wp-types check if parent has child elements
// https://toolset.com/forums/topic/how-to-display-the-child-value-in-a-parent-list-view/
function child_exists_func( $atts ){
$child_posts = types_child_posts('termine');
if ($child_posts) {
//return true;
foreach ($child_posts as $child_post) {
//echo $child_post->wpcf-termin-von;
return $child_post->wpcf-termin-von;
}
}
}
add_shortcode( 'child-exists', 'child_exists_func' );
- Loop item in view:
[wpv-conditional if="( '[child-exists]' gt 'TODAY()' )"]text here[/wpv-conditional]
Hi, Thanks for contacting Toolset support forums.
If I understand correctly you may need to change your code to this:
function child_exists_func( $atts ){
$child_posts = types_child_posts('termine');
if ($child_posts) {
return types_render_field( "wpcf-termin-von", array("post_id" => $child_posts[0]->ID) );
}
}
add_shortcode( 'child-exists', 'child_exists_func' );
The code above assumes your parent post only has one child, which is the one you're willing to apply the condition on. types_render_field renders the field value and returns it, you can read more about it here: https://toolset.com/documentation/user-guides/displaying-wordpress-custom-fields/
Please let me know if that solves your problem.
Cheers!
Hi, thanks for your feedback.
- The parent post can have one or more child posts.
- if the parent post has child post/s and the custom datefield "termin-von" of one or more of the child posts is in the future, i will output nothing in the view
- if the parent has no child post/s or one or more of the child post custom datefield "termin-von" is today or in the past, i will output a text like "request now".
Can you please update the snippet?
is it better to make this condition in the php snippet oder in the view with a [wpv-conditional]?
here is a screenshot of the view output for better understanding
Hi,
Thanks for your reply. In order to achieve what you've described above, you'll need to calculate the timestamp of a date field, and create a shortcode for the calculation result and use it within the wpv-conditional shortcode.
If you need help to achieve this you may contact one of our certified partners, and they'll be more than happy to help you, for more info please refer to this link: https://toolset.com/consultant/
Cheers!