Home › Forums › Add-ons › Repeater Field › Empty Group have_rows is true?
I’m using a Group field to organize a section of fields and I have a post that is saved, but has no sub fields in the group. Still when I call the have_rows
method on that group it is returning a value of true
. Why?
I haven’t really used a group field before and I just did some quick testing. It appears that a group is basically a repeater with 1 row and the field always has 1 row if the post or admin page it’s on has been saved since the field in the group will always have a value, even if the value is false/null/empty or whatever.
That has been my experience as well. So what I’m wondering now is, is this a bug or intended functionality? Since, as you said, it’s like a Repeater and the check seems to be have_rows
no rows in a Repeater is false
yet here with a Group
evaluates to true
. I would argue it’s a bug.
I think that it’s intended.
If the post has never been saved then the group has no rows but if it has been saved it will always have one row even if all the fields in the group have no value.
I think that using have_rows() is just a convenience so that you can reference the fields without the group field name prefix.
For example, lets say that you have a group field with the name of “group” and it has 2 sub fields; “field_1” and “field_2”.
You can access these fields like this:
the_field('group_field_1');
the_field('group_field_2');
or for the convenience of not added the field names together you can do:
while (have_rows('group')) {
the_row();
the_sub_field('field_1');
the_sub_field('field_2');
}
have_rows()
will always return true if the post has been saved.
Think of a regular repeater that has several sub fields but none of them are required. If you add a row to this repeater but do not enter any values into it the empty fields will be saved. This repeater will have one row even though no values have been entered into the existing row. The group field works the same way.
If you think this is a bug you are free to open a new support ticket https://support.advancedcustomfields.com/new-ticket/
The topic ‘Empty Group have_rows is true?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.