Support

Account

Home Forums Front-end Issues get_field_object() add value to select all

Solving

get_field_object() add value to select all

  • Hey,
    I am building a search form.
    I load the acf select options to the search form with get_field_object() function.
    Is it possible to add to the search form a select all option that will return an empty value?

    example
    This is what I have

    <select>
       <option value="apple">Apple</option>
       <option value="banana">Banana</option>
       <option value="onion">Onion</option>
    </select>

    This is what i want to do:

    <select>
       <option value="">All fruits</option>
       <option value="apple">Apple</option>
       <option value="banana">Banana</option>
       <option value="onion">Onion</option>
    </select>
  • You can use the prepare_field filter for that:

    
    <?php
    
    add_filter('acf/prepare_field/type=select', 'add_that_all_option');
    function add_that_all_option($field) {
        if (strpos($field['wrapper']['class'], 'acf-need-to-add-all-option') === false) {
            return $field;
        }
    
        $field['choices'] = array_merge(['' => 'All'], $field['choices']);
    
        return $field;
    }
    

    Note that acf/prepare_field/type=select filter will affect all the select field. That’s why the function first check if the field has the class ‘acf-need-to-add-all-option’ (you can rename it to whatever you want). So, the ‘All’ choices will only be prepend if you added the class in the field setting.

    If this thing only needs to happen to 1 field, you can use these filters instead:
    acf/prepare_field/name=YOUR_FIELD_NAME or acf/prepare_field/key=YOUR_FIELD_KEY

    https://www.advancedcustomfields.com/resources/acf-prepare_field/

    Cheers

  • Thank you very much for your detailed answer.
    But for some reason something is not working.

    I added the code you posted in functions.php file

    I added the class name acf-need-to-add-all-option to the field at the acf field management.

    This is the code I retrieve the field options to the search form at the front end and where I want to add all the ‘select all’ option.

                        <div>
                        <label for="field_59ae96d970a06">Select a city</label>
                            <?php 
                            $field_key = "field_59ae96d970a06";
                            $field = get_field_object($field_key);
                        
                            
                            if( $field )
                                    {
                                        echo '<select name="' . $field['key'] . '">';
                                        
                                            foreach( $field['choices'] as $k => $v )
                                            {
                                                echo '<option value="' . $k . '">' . $v . '</option>';
                                            }
                                        echo '</select>';
                                    }
                                ?>
                            </div>

    To be honest I am sure that I am doing something wrong.
    *** The code you wrote above adds the All option to the backend edit field section.
    How can I add it to the front end?

    Thank you again.

  • Oh, i was under the assumption that you are using the acf frontend form. My bad.

    The hook ‘prepare_field’ only get called on the acf’s rendered form.

    But if you are rendering the form yourself, you can try ‘load_field’ hook instead.
    Like this: acf/load_field/key=field_59ae96d970a06

    https://www.advancedcustomfields.com/resources/acf-load_field/

    Cheers

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘get_field_object() add value to select all’ is closed to new replies.