Home › Forums › Add-ons › Repeater Field › Using repeater for look up not working
I have a very simple repeater field set up that includes on text field and a wysiwyg field.
The idea is to use a function to pass an id to that looks through all the items in the repeater and matches the passed id with the text field and then returns the relevant wysiwyg content:
function get_column_content($columnId){
while(have_rows("adv_columns")){
the_row();
if(get_sub_field('column_id')==$columnId){
return get_sub_field('content');
}
}
}
The strange thing is that the loop never resets between function calls. So if I’ve set up the repeater to have column1, column2, column3 as the ids, when I call the function to look for column1 it works the first time but after that it won’t find it because it seems to be past the row and doesn’t start from the beginning for the next search.
How can I reset the have_rows each time the function is called?
If it matters the functions is inside and called from a class.
Thanks for any help.
Kind of solved:
If I prevent the function from break-ing out of the loop once I’ve found the element I need the process works correctly.
If I break out at the point of finding the element I need the next time the function runs it continues where it left off in the previous loop and therefore misses repeated searches.
function get_column_content($columnId){
while(have_rows("adv_columns",get_the_ID())){
the_row();
if(get_sub_field('column_id')==$columnId){
$found= get_sub_field('content');
}
}
return $found;
}
You are almost there, if you’re going to loop through the same repeater again then you should probable reset the rows as well. Or you may be able to break out of the loop if you reset the rows. You could try something like this.
function get_column_content($columnId){
if (have_rows("adv_columns",get_the_ID())){
while(have_rows("adv_columns",get_the_ID())){
the_row();
if(get_sub_field('column_id')==$columnId){
$found= get_sub_field('content');
break; // found it, don't continue looking
}
}
reset_rows();
} // end if have rows
return $found;
}
The topic ‘Using repeater for look up not working’ 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.