Support

Account

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.