ACF Repeater loops and resets - where is the reset_rows() documentation?
I love ACF, its great for developers and it seems to have so much support. BUT... Why is reset_rows() not in the documentation? I created a function that can delete repeater rows at different times in the code. I was using the rows loop each time expecting it to start at index 0 (or 1 in acf's crazy way). But each loop started where the previous one left off.
This gave me grief for quite a while. I have only found ONE other topic with a small 3 word reply mentioning reset_rows(). ACF documentation has nothing that comes up when you searched reset_rows() or anything about reseting the row index. ITS NO WHERE!
So maybe this can save others from the grief. Here is how i use it successfully:
reset_rows('field_name', $post_id);
$post_id could be optional, havnt tried it..
Here is how i have placed it in my code
if( have_rows('related_trustees', $memberID) )
{
reset_rows('related_trustees', $memberID); // Start from begining (protects against previous loops)
echo 'Have rows';
$rowNum = 0; // ACF Rows start at index of 1
while( have_rows('related_trustees', $memberID) )
{
the_row();
$rowNum ++;
if(get_sub_field('trustee') == $userID)
{
$delrow = delete_row('related_trustees', $rowNum, $memberID);
break;
}
}
}
Let me know if theres a reason to this madness please.
Topic wp-reset-postdata advanced-custom-fields get-row reset loop Wordpress
Category Web