Home Forums Front-end Issues Empty group always returns true


Empty group always returns true

    • tylerkd

    • October 17, 2017 at 11:12 am

    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

        [background] => 
        [text_top] => 
        [text_bottom] => 
        [link] => 

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

    • tylerkd

    • October 17, 2017 at 11:14 am

    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
    • tylerkd

    • October 18, 2017 at 5:51 am

    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!

    // 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>
    	<style type="text/css">
    		#hero {
    			background: <?php echo $hero['color']; ?>;
    <?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

    • davemac

    • January 9, 2019 at 11:03 am

    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.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.