Support

Account

Home Forums Add-ons Repeater Field Know the number of repeater rows before while statement

Solved

Know the number of repeater rows before while statement

  • Hi,
    It looks like some found that this solution works (not nested repeater) :
    echo count( get_field('repeater_slug') );

    But it doesn’t. It displays a 0 whatever the number of rows is.

    So I’d like to know how people who said it works are getting it working ? Yes I’m asking about details, because just giving a line of code is not a complete solution.

    Thanks a lot guys !

  • You say (not nested repeater), are you working with a nested repeater?

    One line of code is the solution. When you use get_field() for a repeater it returns an array. Each row of the repeater is an element of the array. count() gives the number of rows in the array.

    If you’re getting zero for a repeater that has rows then something else is going on preventing you from correctly getting the field.

  • Thanks John, and no I’m not working with a nested repeater.
    This repeater is in a tab, there’s no particular extra-settings.
    You maybe have a beginning of a solution. If I echo the repeater, I get nothing. If I var_dump the repeater, I get NULL. But, I can iterate on it and get all the values.

  • Found !
    It was a mistake by my side. I forget to set the ID in the get_field. In my case I need to pass the ID of a post and I missed this one because I don’t need to specify it in the while statement.

    So it works.

  • I’ve seen this behavior reported here before, but I’ve not seen a solution that works for everyone because I’ve never been able to recreate it.

    Usually, 90+% of the time, when get_field() is not returning the correct value it is due to some other filter on your site. Most of the time it is a pre_get_posts filter.

    The best advice I can give you is the standard WP shtick, disable plugins and switch themes to see if you can figure out what might be interfering.

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

The topic ‘Know the number of repeater rows before while statement’ is closed to new replies.