Support

Account

Home Forums General Issues Set a field's value to null or other value?

Solving

Set a field's value to null or other value?

  • Hi,

    I want to have two fields. One (field A) that is a yes or no choise typ. If that field (field A) is Yes then another field (field B) will be shown and get a default value. If Field A is no then field B is set to a defauld value or null.

    Is this possible with the free subscription through the user interface? Or through code? I’ve seen that you can do changes to the plugin, would anyone know a video or other source to learn how to make those changes?

    Kind Regards

    David

  • I don’t know of any video. The steps are not difficult.

    You create an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/

    In this filter you get the value of the true/false field and then you can use update_field() https://www.advancedcustomfields.com/resources/update_field/ to change the value of the other field based on what the true/false field value is.

  • Hi John,

    Thank you very much for your advice. I tried the following code unfortunately without luck.. Nothing changes. Im a beginner at this so I was wondering if you or someone else can advice me on what is wrong?

    function my_acf_save_post( $post_id ) {

    // Bail early if no data sent.
    if( empty( $_POST[‘are_we_counting_participants’] ) ) {
    return;
    }

    // Get values.
    $values = $_POST[‘are_we_counting_participants’];

    // Update with new value.
    if( $values == ‘no’ ) {
    update_field(‘number_of_participants’, NULL);
    }

    }

    add_action(‘acf/save_post’, ‘my_acf_save_post’);

  • The first thing you need to do is to set up the two fields

    field a = true false field
    field b = this field has conditional logic to only show if field a is true, set the default value of field b to whatever you want as the default.

    
    add_action('acf/save_post', 'my_acf_save_post;, 20);
    function my_acf_save_post($post_id) {
      if (get_field('field_a', $post_id)) {
        // value is true
        // do nothing, value is set to the default or something new was entered
      } else {
        // value is false
        // you can do one of the following
        // set field b to a default value
        update_field('field_b', 'your value', $post_id);
        // or delete the value of the field
        delete_field('field_b', $post_id);
      }
    }
    
  • Hi John!
    Thank you very much for your kind help! It almost worked! The b value (Are we counting participants) is set to NULL. But I realised that the true/false field does not work with the Condition Logic setting (In my field number of participants (b) I have: Show this field if -> Are we counting participants (a) -> Value is equal to -> Checked).
    But when checked it does not show field b (number of participants).

    Please share your ideas of what I could if possible.

    Wish you a great day!
    David

  • Forgot to add the code, here it comes (I tried delete and then the update):

    function my_acf_save_post($post_id) {
    if (get_field(‘are_we_counting_participants’, $post_id)) {
    // value is true
    // do nothing, value is set to the default or something new was entered

    } else {
    // value is false
    // you can do one of the following
    // set field b to a default value
    update_field(‘number_of_participants’, NULL, $post_id);
    // or delete the value of the field
    // delete_field(‘number_of_participants’, $post_id);
    }
    }
    add_action(‘acf/save_post’, ‘my_acf_save_post’, 20);

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

The topic ‘Set a field's value to null or other value?’ is closed to new replies.