Support

Account

Home Forums General Issues Populate choices of a Select Field Type using SQL query

Solving

Populate choices of a Select Field Type using SQL query

  • Hello,

    I have a country/city/towns table within my database that I would like to use to dynamically populate the ‘Choices’ in a ‘Select’ Field Type in my WordPress site.

    Does anyone know how to dynamically populate choices within custom fields?

    Thank you in advance.

  • there is a tutorial for populating the choices of a select field here http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    this can be used for any type of field that has choices like radio and checkbox fields as well as some others.

  • 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.

  • Sorry John. Just further to the above, I need to be able to keep the database table’s relationship between the states, cities, towns, postcodes etc. Not sure how the Options page would deal with that?

  • The options page use on that tutorial is just an example, I was basically refering to that article for the example of how to add the values for $field[‘choices’].

    As far as keeping states, cities and towns associated, that’s a different story and not easy to do in ACF.

    I’m assuming that you mean that when a state is selected that the cities in that state would show in the city field etc. If that’s the case then you’ll need to add your own javascript for the fields and basically convert an ACF select field into something similar to a post object or relationship field that uses select2. That’s beyond what I can help you with.

    My original answer was for what you asked, can you dynamically generate the choices of a select field.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Populate choices of a Select Field Type using SQL query’ is closed to new replies.