Maaz
Maaz

Reputation: 4303

code checks one checkbox or the other, but not both

So basically, I have a function that is ran when a post is created on wordpress. I've modified this function by adding the following code.

$episodeID = $_POST['item_meta'][137];
$episodeVersion = $_POST['item_meta'][357];



if ($episodeVersion == "Subbed") { 
    $value = array("Subbed");

    update_field('field_48', $value, $episodeID);

} 
else if ($episodeVersion == "Dubbed") { 
    $value = array("Dubbed");

    update_field('field_48', $value, $episodeID);



}

This code basically says, if the value of a field in that post is "Subbed" then update another checkbox field by checking the "Subbed" checkbox. If it's "Dubbed" then update the checkbox field by selecting the "Dubbed" checkbox.

This works perfectly fine, however there is no time when both of these checkboxes are checked. If I add a post with the Dubbed, it'll check dubbed, then if I add a post with Subbed, it'll uncheck dubbed and check subbed.

So basically, how can I make it so it doesn't actually uncheck whatever has already been checked. So what should I use to check to see if the checkbox is unchecked or checked? Some type of boolean true / false?

Upvotes: 0

Views: 138

Answers (1)

Maaz
Maaz

Reputation: 4303

Okay, so I was able to manipulate some of the code and get it to work eventually.

All I had to do was check if the Subbed checkbox was already checked when executing the dubbed if statement, and vice versa.

Here is the updated code

$episodeID = $_POST['item_meta'][137];
$episodeVersion = $_POST['item_meta'][357];



if ($episodeVersion == "Subbed" && !(get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion', $episodeID))) ) { 
    if (get_field('episode_sdversion') && in_array( 'Dubbed', get_field('episode_sdversion'))) {    
        $value = array("Subbed", "Dubbed");         
    } else {
        $value = array("Subbed");
    }       

    update_field('field_48', $value, $episodeID);

} 
if ($episodeVersion == "Dubbed" && !(get_field('episode_sdversion') && in_array('Dubbed', get_field('episode_sdversion', $episodeID))) ) { 

    if (get_field('episode_sdversion') && in_array( 'Subbed', get_field('episode_sdversion'))) {    
        $value = array("Subbed", "Dubbed");         
    } else {
        $value = array("Dubbed");
    }

    update_field('field_48', $value, $episodeID);



}

Upvotes: 0

Related Questions