Advanced Custom Fields plugin User dropdown is not working

Viewed 425

I am using the ACF plugin in one of the WordPress websites and I have the following configuration for a user dropdown. This is added as a PHP code.

acf_add_local_field_group(array (
      'key' => 'group_557ad1ef2b8a1',
      'title' => 'Authors',
      'fields' => array (
        array (
          'key' => 'field_557ad24ba99e5',
          'label' => 'Featured',
          'name' => 'featured_authors',
          'type' => 'repeater',
          'instructions' => 'Choose a maximum of 6 authors',
          'required' => 0,
          'conditional_logic' => 0,
          'wrapper' => array (
            'width' => '',
            'class' => '',
            'id' => '',
          ),
          'min' => '',
          'max' => 6,
          'layout' => 'table',
          'button_label' => 'Add Author',
          'sub_fields' => array (
            array (
              'key' => 'field_5582bc82f8419',
              'label' => 'Author',
              'name' => 'featured_authors_author',
              'type' => 'user',
              'instructions' => '',
              'required' => 1,
              'conditional_logic' => 0,
              'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
              ),
              'role' => '',
              'allow_null' => 0,
              'multiple' => 0,
            ),
          ),
        )
      'menu_order' => 0,
      'position' => 'normal',
      'style' => 'seamless',
      'label_placement' => 'top',
      'instruction_placement' => 'label',
      'hide_on_screen' => array (
        0 => 'the_content',
        1 => 'author',
        2 => 'featured_image',
      ),
    ));

Now from the admin section, I am trying to add this field to the page, but it is not permitting the admin to select users. The dropdown is showing without any values. I am using the following versions:

Wordpress - 5.7.3

Advanced Custom Fields - 5.4.5

1 Answers

You have a syntax error in your code. You didn't close the first array() inside the 'fields' parameter in your main list of fields. Before this line:

'menu_order' => 0,

you need to add this:

),

This will close the 'fields' parameter, and allow it to process the parameters that appear after it.

I ended up recreating your field group in the ACF User Interface in the WP Admin, then exported the php, stripped out all the unnecessary/default parameters, and it's working great for me on a test environment. See the code below. I stripped out all of the settings that were just set to the default settings, to help make things a bit more readable.

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_6148d6a5d8172',
    'title' => 'Authors',
    'fields' => array(
        array(
            'key' => 'field_6148d6afde7ec',
            'label' => 'Featured',
            'name' => 'featured_authors',
            'type' => 'repeater',
            'instructions' => 'Choose a maximum of 6 authors',
            'max' => 6,
            'button_label' => 'Add Author',
            'sub_fields' => array(
                array(
                    'key' => 'field_6148d6d5de7ed',
                    'label' => 'Author',
                    'name' => 'featured_authors_author',
                    'type' => 'user',
                    'required' => 1,
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
    'style' => 'seamless',
    'hide_on_screen' => array(
        0 => 'the_content',
        1 => 'author',
        2 => 'featured_image',
    ),
));

endif;

One thing I noticed is that your code doesn't have any Location settings to dictate what post type or other element the field group should appear on, so I set my example to appear on every Post. You may need to adjust this to match your needs.

I also included the if function_exists conditional wrapper so that if you turn off the ACF plugin, it won't generate errors when it cannot find the acf_add_local_field_group() function.

Related