Home Forums Backend Issues (wp-admin) Supply a foreach to ACF Select Field


Supply a foreach to ACF Select Field

  • So I have the form bit of a WP Core Widgets class that I am working on and I’m stuck on wanting to supply a list that’s already populated to a Advanced Custom Field ‘select’ field.

    Instead of lists, how can I supply and save the drop-down results to acf_lists?

        // This is the backend of the widgets
        public function form($instance)
            // Grab the widget ID
            $widget_id = $this->id;
            $acf_lists = get_field('mailchimp_list', $widget_id) ? get_field('mailchimp_list',$widget_id) : '';
            // Grab the MailChimp_API getters and setters
            $api = mailchimpSF_get_api();
            // If there is no API, return
            if (!$api) {
            // If there is an API Key, proceed
            if ($api) {
                $lists = $api->get(
                        'fields' => ',,lists.email_type_option'
                $lists = $lists['lists'];
                // If there are no lists present
                if (count($lists) == 0) {
                    <span class='error_msg'>
                    echo sprintf(
                        esc_html("Uh-oh, you don't have any lists defined! Please visit %s, login, and setup a list before using this tool!"),
                        "<a href=''>MailChimp</a>"
                } else {
                    <table class="mc-list-select">
                        <tr class="mc-list-row">
                                <select name="mc_list_id">
                                    <option value=""> &mdash; <?= 'Select A List' ?>
                                    foreach ($lists as $list) {
                                        $option = get_option('mc_list_id');
                                        <option value="<?= $list['id'] ?>"<?= selected($list['id'],
                                            $option); ?>><?= $list['name'] ?></option>
                                <input type="hidden" name="mcsf_action"
                                <input type="submit" name="Submit"
                                       value="<?= 'Update List' ?>" class="button"/>

    Here is what the widget looks like:

    [![enter image description here][1]][1]

    How can I supply the current foreach that I have into the $acf_lists variable? I’m able to loop through and supply it to a standard drop-down but I’m unsure how I can supply it to the ACF dropdown.

    This is my ACF Mailchimp list select field:

    [![enter image description here][2]][2]


Viewing 1 post (of 1 total)

The topic ‘Supply a foreach to ACF Select Field’ is closed to new replies.