Home › Forums › Add-ons › Options Page › Multiple Options Pages w/ Multiple Instances of Dynamic Choices
I have two options pages (page A and page B), and I’ve set up a dynamically populated dropdown on Page B that pulls information from Page A. I’d like to set up a dynamically populated dropdown on Page A that pulls information from Page B, but the two dropdowns don’t seem to play well with each other. I can get each to work if they’re isolated, but I can’t run both functions at the same time.
I thought maybe it was a restated variable issue, but even making the variables unique didn’t get them to work concurrently. Please help!
Here is my code (with specifics changed)
// Runs on page B, gets select choices from Page A
function acf_load_uno_field_choices( $field1 ) {
// reset choices
$field1['choices'] = array();
// if has rows
if( have_rows('repeater1', 'option') ) {
// while has rows
while( have_rows('repeater1', 'option') ) {
// instantiate row
the_row();
// vars
$label1 = get_sub_field('textfield1');
$value1 = strtolower($label1);
// append to choices
$field1['choices'][ $value1 ] = $label1;
}
}
// return the field
return $field1;
}
add_filter('acf/load_field/name=dropdown1', 'acf_load_uno_field_choices');
// Runs on page A, gets select choices from Page B
// reset choices
$field2['choices'] = array();
// if has rows
if( have_rows('repeater2', 'option') ) {
// while has rows
while( have_rows('repeater2', 'option') ) {
// instantiate row
the_row();
// vars
$label2 = get_sub_field('textfield2');
$value2 = strtolower($label2);
// append to choices
$field2['choices'][ $value2 ] = $label2;
}
}
// return the field
return $field2;
}
add_filter('acf/load_field/name=dropdown2', 'acf_load_dos_field_choices');
It is very likely that you’re creating an infinite loop.
1) When ACF loads repeater 1 the filter to get the values from field 2 is called
2) This causes ACF to load repeater 2
3) When ACF loads repeater 2 the filter to get the values from field 1 is called
4) This causes ACF to load repeater 1 — loop back to #1
This can be solved in a couple of ways.
1) If you are familiar with how ACF stores the values of a repeater you can use the WP function get_option(), this avoids calling ACF functions and eliminates the infinite loop.
// example of getting repeater values without ACF functions
$repeater = 'repeater1'; // name of repeater
$count = intval(get_option('options_'.$repeater)); // gets # rows
for ($i=0; $i<$count; $i++) {
$value = get_options('options_'.$repeater.'_'.$i.'_sub_field_name';
}
2) In each filter, remove the hook for that filter and then replace it so that each filter can only be called oned.
// example of removing/replacing filter
function acf_load_uno_field_choices($field1) {
// remove this filter
remove_filter('acf/load_field/name=dropdown1', 'acf_load_uno_field_choices');
// your code to get values here
// re-add this filter
add_filter('acf/load_field/name=dropdown1', 'acf_load_uno_field_choices');
// return field
return $field1;
}
add_filter('acf/load_field/name=dropdown1', 'acf_load_uno_field_choices');
The topic ‘Multiple Options Pages w/ Multiple Instances of Dynamic Choices’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.