Home › Forums › Backend Issues (wp-admin) › Dynamically Load Select Choices with Functions
Hello,
I’m loading ACF fields through a functions file. I’m trying to dynamically populate choices for a select field. I’ve done this successfully once using 'choices' => get_all_sites(),
which shows in a var_dump (in single.php) to return array(5) { ["NULL"]=> string(19) "NULL (Waiting List)" [2]=> string(53) "RS of the SBB" [3]=> string(46) "RS of the IM" [4]=> string(47) "RS of the SMM" [5]=> string(49) "RS of the HHM" }
I’m trying to do something similar with 'choices' => get_waiting_lists(),
which shows in a var_dump (in single.php) to hold array(2) { [449]=> string(26) "Waiting List - SMM" [623]=> string(29) "Waiting List - SBB" }
.
I cannot for the life of me figure out why this second array won’t populate select choices. Does this make any sense to you or do you have any ideas? Or should I maybe be using a filter instead?
Thank you!!
Most likely, the issue is that the function does not work at the time when you are using it in the functions.php file.
Can you check by debuging the value of get_waiting_lists()
in the same file as your field group code? Does it work there, or only in single.php?
If my suspicions are correct, you will need to use a filter to set the choices. If you take a look on the docs page, you will see an article which covers this exact query.
Thanks
E
I guess I need to use a filter. I modified the function to the following (so $field holds that array I was talking about in the initial post) and added the filter but still no luck… Any ideas? I really appreciate your help!
function get_waiting_lists( $field ) {
$field = array();
// Query Waiting Lists Products
$args = array(
'post_type' => 'memberpressproduct',
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'topics',
'field' => 'id',
'terms' => '22',
'operator' => 'IN'
),
)
);
$lists_array = get_posts( $args );
foreach ( $lists_array as $list ) {
$field[$list->ID] = $list->post_title;
}
wp_reset_postdata();
return $field;
}
add_filter('acf/load_field/name=select_waiting_list', 'get_waiting_lists');
Your code is showing in incorrect use of the $field variable.
This variable is not just for choices, it is for many pther things such as name / label etc.
You need to be modifying the $field[‘choices’], not $field.
Please let me know if this was not clear in the docs.
Thanks
E
Sorry about that – I believe we have the correct use of the variable now, but it doesn’t seem to be working correctly. Here’s everything I know:
'name' => 'select_waiting_list',
with 'choices' => array(),
in functions.php.function get_waiting_lists( $field ) {
$field['choices'] = array();
// Query Waiting Lists Products
$args = array(
'post_type' => 'memberpressproduct',
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'topics',
'field' => 'id',
'terms' => '22',
'operator' => 'IN'
),
)
);
$lists_array = get_posts( $args );
foreach ( $lists_array as $list ) {
$choices[$list->ID] = $list->post_title;
}
$field['choices'] = $choices;
wp_reset_postdata();
return $field;
}
add_filter('acf/load_field/name=select_waiting_list', 'get_waiting_lists');
array(1) { ["choices"]=> array(2) { [449]=> string(26) "Waiting List - Millionaire" [623]=> string(29) "Waiting List - Small Business" } }
<select id="acf-field-connect_waiting_list" class="select" name="fields[field_5272b350a4105]"><option value="null">- Select -</option><option value="choices"></option></select>
Thank you again, so much, for your help sorting this out. You guys have such great support.
– Ross
It is possible that there is a bug at the moment which would prevent the load_field filter from working witha field name. Can you try using the field key instead?
In your filter, just change ‘name=select_waiting_list’ to ‘key=field_123’ – where this is the field key.
Also, you can check if the filter is working by placeing this code before the return $field;
print_r( $field );
die;
Thanks
E
Banging my head on the wall now. name= works. I made a stupid mistake and had a duplicate field in my functions with a slightly different name and I was calling the wrong one. Thanks so much elliot! It’s great to understand the filters now.
The topic ‘Dynamically Load Select Choices with Functions’ 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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.