advanced custom fields – ACF select acf/load_field filter to render choices with option groups

Question

I am using ACF select field and allowing multiple choices, with lazy loading etc.

Here is my ACF load_field filter fired within my class construct (hence the $this usage)…

add_filter('acf/load_field/name=provision', [ $this, 'load_provision_choices' ]);

Here is the class function php in reference to the code above…

/**
 * @param $field
 * @return array
 * @noinspection PhpUnused
 */
public function load_provision_choices( $field ) {

    // current site key (purchase)
    $site = Site::key();

    // reset choices
    $field['choices'] = [];

    // add our place holder
    $field['placeholder'] = 'Select provisional items...';

    // if we have site key
    if($site) {

        // switch to site (data)
        Site::switch('data');

        // our post type option group breaks
        $groups = [
            'data-packaging' => 'Packaging',
            'data-confectionery' => 'Confectionery',
            'data-components' => 'Components',
            'data-shipping' => 'Shipping',
            'data-materials' => 'Materials'
        ];

        // loop through groups as type => label
        foreach ($groups as $type => $label) {

            // provision args
            $args = [
                'numberposts'   => -1,
                'post_type'     => $type,
                'orderby'       => 'title',
                'order'         => 'ASC',
                'post_status'   => 'publish'
            ];

            // get current data posts
            $provisions = get_posts($args);

            // if we have provisions
            if($provisions) {

                // foreach provisions
                foreach ($provisions as $provision) {

                    // add provision to field select choices
                    $field['choices'][$label][$provision->ID] = $provision->post_title;

                }

            }

        }

        // switch to site (purchase)
        Site::switch($site);

    }

    dump($field);

    // return the field
    return $field;

}

Which returns this…

Array
(
    [ID] => 0
    [key] => field_60a44ccc4e40a
    [label] => Line Items
    [name] => provision
    [prefix] => acf
    [type] => select
    [value] => 
    [menu_order] => 0
    [instructions] => Select line item or items for this provision included in contract agreement.
    [required] => 1
    https://wordpress.stackexchange.com/q/388383 => 
    [class] => 
    [conditional_logic] => 0
    [parent] => field_60a44b724e409
    [wrapper] => Array
        (
            [width] => 
            [class] => 
            https://wordpress.stackexchange.com/q/388383 => 
        )

    [choices] => Array
        (
            [Packaging] => Array
                (
                    [24] => Maxi Round Pot
                )

            [Confectionery] => Array
                (
                    [89] => The Jelly Bean Factory
                )

        )

    [default_value] => Array
        (
        )

    [allow_null] => 0
    [multiple] => 1
    [ui] => 1
    [ajax] => 1
    [return_format] => value
    [placeholder] => Select provisional items...
    [_name] => provision
    [_valid] => 1
)

But unfortunately the loaded select field in question shows no values.

This is what the ACF field editor select choices show…

enter image description here

Does anyone have insight on my error above and how to pre load select options with option group divisions in ACF select choice array.

Thanks in advance!

0
joshmoto 2 months 2021-05-18T20:53:44-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse