Skip Navigation

[Resolved] (PHP) check if types_render_field is empty in a repeatable loop

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

Problem:

Query Repeatable Field Groups by custom field value using PHP codes.

Solution:

The Toolset Repeatable Field Groups are based on one-to-many relationship, for example:

https://toolset.com/forums/topic/php-check-if-types_render_field-is-empty-in-a-repeatable-loop/#post-1849545

Relevant Documentation:

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

This support ticket is created 4 years, 6 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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by sabineM-2 4 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1849521
Bildschirmfoto 2020-11-19 um 10.25.43.png
Bildschirmfoto 2020-11-19 um 10.25.31.png

Hello support team,

i am trying to display a repeatable field in php with a single types render field.
This works fine, but i want it to show the whole content only if one of the fields is filled.

Here my example:

<?php if(!empty(types_render_field('video-link-proj'))){?>

<div class="container">
<span class="line-through">
</span>
<h2 class="text-center">Videos</h2>
</div>

<div class="container" id="videos">

<?php // Main Features
$args = array(
'post_type' => 'videos-proj',
'numberposts' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => get_the_ID(),
'relationship' => 'videos-proj',
),
'meta_key' => 'toolset-post-sortorder',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ){
$the_query->the_post(); ?>
<div class="six columns">
<iframe width="100%" height="290" src="hidden link;?php echo types_render_field('video-link-proj') ;?>" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

</div>

<?php
}
?>
<?php wp_reset_query();?>
</div>
<!--Video-Con-->
<?php }?>

In this case i try to show the headlines (outside the loop) only if the renderfield is filled.
This is not working. A screenshot of my backend is attached to this.

The site:
hidden link

#1849545

Hello,

The Toolset Repeatable Field Groups are based on one-to-many relationship, you can not check the child post fields in parent post.

In your case, you just need to add a custom field filter in the query, for example:

<?php // Main Features
$args = array(
	'post_type' => 'videos-proj',
	'numberposts' => -1,
	'toolset_relationships' => array(
	'role' => 'child',
	'related_to' => get_the_ID(),
	'relationship' => 'videos-proj',
	),
	'meta_key' => 'toolset-post-sortorder',
	'orderby' => 'meta_value_num',
	'order' => 'ASC',
	// Add below field filters
    'meta_query' => array(
        array(
            'key'     => 'wpcf-video-link-proj',
            'value'   => '',
            'compare' => '!=',
        ),
    ),
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ){
$the_query->the_post(); ?>
<div class="container">
Here display the whole content
</div>
<?php
}
?>
<?php wp_reset_query();?>

More help:
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

#1862435

thank you for the support!