Home Forums General Issues Setting ACF true/false field value automatically


Setting ACF true/false field value automatically

  • I have an ACF custom post type Business Listing with a group of custom fields. Site is created with Elementor Pro

    One of the fields is a true/false field to flag whether the business is to be featured in the directory or not

    This is determined by the membership level of the user creating the business listing through a front end form i.e. their user role.

    I have set the default value for the field to false (in the ACF admin interface it shows an empty checkbox).

    BUT if I create a form without that field in it (for my lower level members) then the post they create doesn’t get a value at all – there is no post meta record written into the database for the “featured” field. The only point at which the record gets written is if I edit and save the post again (then it gets a post meta record with a 0

    On the version of the form for higher level members, I can’t find a way in the ACF New Post form (or the Front End Admin forms) to prefill the checkbox. I don’t want them to end up with their listing not featured because they didn’t notice the checkbox.

    My business listings archive page sorts by whether they are featured or not, then post title. But any listings that are created without a post meta value for the true/false field get sorted ahead of the featured ones.

    So can anybody point me to:

    1. A way of ensuring that a post meta record IS created for a form that doesn’t have the field in it


    2. A plugin or elementor add on that has a suitable form widget that will let me set these values as part of the form submission process or set them within the form itself or have them hidden but filled.


    3. A code snippet I can set up that will achieve the above

    Thanks so much

  • Create an acf/prepare_field filter

    You will need the field key of this filter. I don’t have all the code

    add_filter('acf/prepare_field/key=FIELD_KEY_HERE', 'FUNCTION_NAME_HERE');
    function FUNCTION_NAME_HERE($field) {
      if (is_admin()) {
        return $field;
      // get the current user level/role and check
      if ($role_can_create_featured) {
        // if the user is able to create a featured post
        // set the value default value to tru
        $field['default_value'] = 1;
        return $field;
      } else{
        // user not allowed to create featured post
        // create a hidden field that matches the acf true false field
        ?><input type="hidden" name="acf[FIELD_KEY_HERE]" value="0" /><?php
        // remove the existing field
        return false;
  • Thanks for the suggestion John

    I’m not entirely sure what to do with this code.

    I’ve put it in Code Snippets and edited it to the correct field key, given my function a name and chosen one of my levels that can be featured and put that role’s name in place of $role_can_create_featured (There are three levels that can so I will need to adapt the code to reflect this)

    But when I view the page that has the front end form on it for that level of membership (built with Dynamiapps Pro), and logged in as a user with that user role, the featured field is displaying but it isn’t ticked.

    I completed the form but left the featured field unticked but my new listing post doesn’t have featured set to true.

    So I’m not sure if your code is running or not – or whether the fact that the form is built in Elementor using the Dynamiapps Front End Admin makes a difference to how this should be approach.

    Hoping you have some further thoughts.

  • More than likely it has to do with elementor, but I don’t know.

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

You must be logged in to reply to this topic.