Trekdrop
Trekdrop

Reputation: 495

How to uncheck an acf checkbox with delete_post_meta

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 );

Part of the code:

Before this part of code I define if the ticketpost must be created or updated. Then the following:

/* Get the current field state */     
    $field_checked = get_field($field_key, $mypostid);             

            /* Check if the posttitle already is checked, if so delete_post_meta */
            if(is_array($field_checked) && in_array($eventname, $field_checked)) { 

              delete_post_meta($RegisterTicket, 'my_custom_field', $eventname );

            }  else {
              /* if not already checked, add the title to the array so it will be checked and update_post_meta */
              $field_checked[] = $eventname;      
              update_post_meta($RegisterTicket, 'my_custom_field', $field_checked );
            }  


        update_post_meta($RegisterTicket, 'user', $TheUserID); 

The delete_post_meta is not working, the checked value is not getting unchecked. How to solve this? Thanks in advance.

Upvotes: 0

Views: 1657

Answers (2)

Trekdrop
Trekdrop

Reputation: 495

Got it working. While ACF checkboxes stores their values in an array we need to unset the value from this array to get it unchecked.

Here is what I did:

$field_checked = get_field($field_key, $mypostid);

Let's say $field_checked = an array of Event 1, Event 2 and Event 5

In this case get_field is an acf function (so it will return Event 1, 2 and 5). We can also use here get_post_meta with corresponding variables. Then the following:

    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);

First we check if the value is stored in an array and we check if the value is already in the array (so we check if it's checked). If yes: We want to unset the variable out of the array. unset works only with a key, so we need to search for the key first with array_search. And then we will unset it, which means we delete the value out of the array.

In short: If $eventname = 'Event 5' $field_checked will be an array of Event 1 and Event 2.

update_post_meta will eventually update the post and save the right array of checkboxes.

Upvotes: 0

David
David

Reputation: 5937

You need to check delete post meta.

Assign the result to a var:

if(is_array($field_checked) && in_array($eventname, $field_checked)) { 

         $result =delete_post_meta($RegisterTicket, 'my_custom_field', $eventname );
         echo '<h1>'.$result.'</h1>';
        }  else {
          /* if not already checked, add the title to the array so it will be checked and update_post_meta */
          $field_checked[] = $eventname;      
          update_post_meta($RegisterTicket, 'my_custom_field', $field_checked );
        }  

if it is false you need to check $RegisterTicket is a post id, 'mycustomfield' and $eventname, one of these will be incorrect. sometimes the var to be deleted is wrong by case. A alternative way to do this is to pull it from the database so there is no error.

$field_checked= get_post_meta($RegisterTicket, 'my_custom_field', true);

or just delete all keys with that field for the post:

delete_post_meta($RegisterTicket, 'my_custom_field');

Upvotes: 2

Related Questions