Support

Account

Home Forums Front-end Issues Empty group always returns true

Solved

Empty group always returns true

  • When using “if (have_rows(‘field_name’))” or “if (get_field(‘field_name’))”, even when the group is empty, true is returned.

    If I print_r() the field I get this

    Array
    (
        [background] => 
        [text_top] => 
        [text_bottom] => 
        [link] => 
    )

    Shouldn’t it return false since the keys have no values?

  • I’m currently using array_filter to get around this but that seems slower than using the normal methods for checking if a field is set.

  • With a group field you need to check for values in each of the fields in the group. A group field is in essence a repeater field that always has exactly 1 row. have_rows() will always return true.

    You are better off using something like

    
    $group = get_field('field_name');
    if ($group['background']) {
      // do something
    }
    
  • Thanks for the reply. I did end up figuring that out. The documentation is confusing to me on this since in the basic example it does an if get_field() check which can only return true making it seemingly useless to check. I guess there are still cases when the template might not have that field so you would want to check if the group exists but that’s not immediately clear to me from the documentation. My incorrect assumption was that the if statement in the documentation was checking if the group had any values inside of it’s fields. It seems in most cases with ACF “if get_field()” usually returns true after the admin has entered some data, not after the field is created in the ACF ui. Anyway, thanks for the reply!

    <?php
    		
    // vars
    $hero = get_field('hero');	
    
    if( $hero ): ?>
    	<div id="hero">
    		<img src="<?php echo $hero['image']['url']; ?>" alt="<?php echo $hero['image']['alt']; ?>" />
    		<div class="content">
    			<?php echo $hero['caption']; ?>
    			<a href="<?php echo $hero['link']['url']; ?>"><?php echo $hero['link']['title']; ?></a>
    		</div>
    	</div>
    	<style type="text/css">
    		#hero {
    			background: <?php echo $hero['color']; ?>;
    		}
    	</style>
    <?php endif; ?>
  • Yes, the documentation on that field is a little confusing because it appears that the loop should not run without values. Sorry, I can’t do anything about the documentation and I don’t know if the effect that we’re seeing is expected behavior or not, if the documentation is wrong of something was overlooked when creating the new group field. The best way to get that answer would be to submit a support ticket here https://support.advancedcustomfields.com/new-ticket/

  • I think it would be really helpful to add this information to the documentation page for the ACF Group field. I spent hours working this out.

  • Agreed, it would be very helpful to have this documented on ACF Group Field page or have clear link on that page to nested groups in have_rows()

    https://www.advancedcustomfields.com/resources/have_rows/

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Empty group always returns true’ is closed to new replies.