Home › Forums › Add-ons › Options Page › Dynamically populate multiple ACF Select fields
I have two fields that i need to populate with custom select options.
Field one is location_name and field two is course_length. They appear multiple times on one page. The fields are displayed on a options page.
When i use the following code to populate the select fields with custom options the page crashes and does not load. ( 404 ).
I cant work out what iv done that causes this to happen.
function acf_load_location_names_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// create array
$store_location_names = [];
// query posts
$args = new WP_Query( array(
'post_type' => 'cpt_opportunities',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft'),
));
// loop through posts
if ( $args->have_posts() ) :
while ( $args->have_posts() ) : $args->the_post();
$location_of_course = get_field('trip_summary_location_of_course');
if(!in_array($location_of_course, $store_location_names)){
array_push($store_location_names, $location_of_course );
}
endwhile; wp_reset_postdata();
else : endif;
// create select field options from array
foreach ( $store_location_names as $location_name) {
$field['choices'][ $location_name ] = $location_name;
}
// return the field
return $field;
}
add_filter('acf/load_field/name=location_name', 'acf_load_location_names_field_choices');
function acf_load_course_lengths_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// create array
$store_course_lengths = [];
// query posts
$args = new WP_Query( array(
'post_type' => 'cpt_opportunities',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft'),
));
// loop through posts
if ( $args->have_posts() ) :
while ( $args->have_posts() ) : $args->the_post();
$get_course_length = get_field('trip_summary_course_length');
if(!in_array($get_course_length, $store_course_lengths)){
array_push($store_course_lengths, $get_course_length );
}
endwhile; wp_reset_postdata();
else : endif;
// create select field options from array
foreach ( $store_course_lengths as $course_length) {
$field['choices'][ $course_length ] = $course_length;
}
// return the field
return $field;
}
add_filter('acf/load_field/name=course_length','acf_load_course_lengths_field_choices');
The first issue that I see is that if you are going to do a loop like this inside of a function
if ( $args->have_posts() ) :
while ( $args->have_posts() ) : $args->the_post();
then you must define must add
global $post;
in your function before you do so.
Do you have debugging turned on so you can see the actual errors that are crashing your page?
You must be logged in to reply to this topic.
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.