PurplePilot
PurplePilot

Reputation: 6612

Dynamically populate a select field in Wordpress ACF from a database table

Right i got a lot of criticism for this question so i will rewrite it. I am using Advanced Custom Fields (ACF) in Wordpress to to build my forms. My particular problem is that in a Select i do not want to pre-populate the field but dynamically fill it at run time from a table in the database.

To build a form group you use ACF's gui to create and define a Form Group, which has one or more Form Fields in it. One this is defined you export the working code and then use this code when the application runs. The exported code is an array of arrays and subarrays that define the characteristics of the Form and cannot contain dynamic code.

As an aside if the items in a field are Post Types then ACF lets you point to and filter by the Post Types giving a sort of Select field. However i do not want to make this particular data a Post Type.

An example of the created code for a hard wired Select Field is as follows.

acf_add_local_field_group(array (
'key' => 'group_568d1e1d7e7fd',
'title' => 'Course Information',
'fields' => array (
    array (
        'key' => 'field_568d1e2d97b99',
        'label' => 'Accrediting Body',
        'name' => 'joltle_course_accrediting_body',
        'type' => 'select',
        'instructions' => '',
        'required' => 1,
        'conditional_logic' => 0,
        'wrapper' => array (
            'width' => '',
            'class' => '',
            'id' => '',
        ),
        'choices' => array (
            0 => '',
            1093 => 'British Institute of Cleaning Science',
            1094 => 'British Oxygen Corporation (BOC)',
            1095 => 'CardianBCT',
            1096 => 'Chartered Institute of Environmental Health',
            1097 => 'Critical Care Institute Manchester',
        ),
        'default_value' => '0',
        'allow_null' => 0,
        'multiple' => 0,
        'ui' => 0,
        'ajax' => 0,
        'placeholder' => '',
        'disabled' => 0,
        'readonly' => 0,
    ),
    array (

It is the 'choices' array that i wish to replace dynamically.

An example of the table and the data i wish to use is as follows.

DROP TABLE IF EXISTS `counties `;

CREATE TABLE counties ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, county varchar(200) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=91 DEFAULT CHARSET=utf8;

INSERT INTO counties (id, county) VALUES (1, 'Bath and North East Somerset'), (2, 'Bedford'), (3, 'Blackburn with Darwen'), (4, 'Blackpool'), (5, 'Bournemouth'), (6, 'Bracknell Forest'), (7, 'Brighton & Hove');

xx

Upvotes: 0

Views: 11830

Answers (1)

Mike B
Mike B

Reputation: 1446

It is in the ACF docs found at http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

function acf_some_field( $field ) {
    //Change this to whatever data you are using.
    $data_from_database = array('key1' => 'value1', 'key2' => 'value2');

    $field['choices'] = array();

    //Loop through whatever data you are using, and assign a key/value
    foreach($data_from_database as $field_key => $field_value) {
        $field['choices'][$field_key] = $field_value;
    }
    return $field;
}
add_filter('acf/load_field/name=what_you_need', 'acf_some_field');

Upvotes: 4

Related Questions