Home › Forums › General Issues › Populate choices of a Select Field Type using SQL query › Reply To: Populate choices of a Select Field Type using SQL query
Thanks John for your speedy reply. So, does this mean it is not at all possible to do the below – include a $wpdb query within the following function?
function acf_load_lga_field_choices( $field ) {
// reset choices
$field['choices'] = array();
global $wpdb;
$choices = $wpdb->get_results( "SELECT local_government_area FROM table_towns" );
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=local_government_area', 'acf_load_lga_field_choices');
I don’t understand how using Options page will help, or what I would need to do with cities/towns table from my database?
Can you please explain a little further, or provide another example, of how the Options page would work to dynamically populate ‘choices’ using existing database content. Thank you.
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.