Hide or exclude an ACF field in a front end form and store default value

Viewed 926

I have a field group attached to a custom post type. It contains a 'status' select input which the user can choose an option from in the wp admin area.

I also have a front end form using the same field group so lower level users can create a post on the front end, however they are not allowed to choose a 'status' they must accept the default.

I setup a default value and made it required, then excluded the field from the front end form using the settings arg. This didn't work, it just left the field value empty.

Alternatively is there a way to output the field as a hidden input on the front end?

1 Answers

The field must appear in the frontend form. If not the value won't be stored in the post_meta table. You have to make it not visible.

You can do like this (here my acf field slug is "id_label") :

// set default value
add_filter('acf/load_field/name=id_label', function($field) {   

    // set a dynamic value if necessary
    $field['default_value'] = '666';

    return $field;
});

// hide the field in frontend form
add_filter('acf/prepare_field/name=id_label', function($field) {
    
    if(!is_admin()) {
        $field['wrapper']['class'] = 'hide';
    }

    return $field;
});
Related