Support

Account

Home Forums General Issues Uncheck checkbox from outside

Unread

Uncheck checkbox from outside

  • Hi,

    I’m working on a wp plugin where people can join/unjoin an event.

    What I’ve done so far:

    I’ve made a custom post type ‘tickets’ with a ACF checkbox that is dynamically populated with post titles of ‘Event’ posts. On a single Event people can click a button which will create a ticket post with the checked value of the ACF checkbox. If the user already has a ticket, it will update the ticket post. This all works great.

    What I want:

    When the user wants to unjoin the event, the acf value must be unchecked. I’ve tried several things like delete_post_meta($RegisterTicket, ‘my_custom_field’, $eventname ); but this didn’t work. Probably because it’s stored in an array.

    Is there another function to uncheck the field value?

    I’ve solved it like this, but I’m not sure if this is the best way:

    $field_checked = get_field($field_key, $mypostid);
    
    if(is_array($field_checked) && in_array($eventname, $field_checked)) { 
          if(($key = array_search($eventname, $field_checked)) !== false) {
               unset($field_checked[$key]);
    
          }
    
        }  else {
    
          $field_checked[] = $eventname;      
    
        }  
    
    update_post_meta($RegisterTicket, 'my_custom_field', $field_checked );
    update_post_meta($RegisterTicket, 'user', $TheUserID);

    Not sure if this is the best way to do it. If so, will there be a delete_value($fieldkey, $value) or something in the future? Thanks!

Viewing 1 post (of 1 total)

The topic ‘Uncheck checkbox from outside’ is closed to new replies.