Stem Step
Stem Step

Reputation: 71

How do I update_post_meta with a checkbox array?

How do I take all the checked options and fill my Custom Post Type Checkbox field?

Here is the form:

    <form action="#" method="post">
          <div class="row">
          <div class="column left labels">
<input type="checkbox" name="grips[]" value="Top (T1)"><label>Top (T1)</label>
<input type="checkbox" name="grips[]" value="High (12)"><label>High (12)</label>
                </div>
              <div class="column left labels">
<input type="checkbox" name="grips[]" value="Mid (23)"><label>Mid (23)</label>
<input type="checkbox" name="grips[]" value="Low (34)"><label>Low (34)</label>
              </div>
              <div class="column left labels">
<input type="checkbox" name="grips[]" value="Bottom (P4)"><label>Bottom (P4)</label>
<input type="checkbox" name="grips[]" value="Other"><label>Other</label>
    </div>
    </div>

<input type="submit" name="submit" value="Submit"/>
</form>

I want to do this, but this doesn't work, it will only take the last value in the array:

__update_post_meta( $the_post_id, 'grips', $_POST['grips']);

I tried to loop through, but that will remove all the other checkmarks and leave only the last one added as well:

foreach($_POST['grips'] as $selected){
    echo $selected."</br>";
__update_post_meta( $the_post_id, 'grips', $selected);  
}

This is what I have in my Functions.php file: I imagine I need another function for arrays since this one only updates single custom fields:

function __update_post_meta( $post_id, $field_name, $value = '' ) {
    if ( empty( $value ) OR ! $value )
    {
        delete_post_meta( $post_id, $field_name );
    }
    elseif ( ! get_post_meta( $post_id, $field_name ) )
    {
        add_post_meta( $post_id, $field_name, $value );
    }
    else
    {
        update_post_meta( $post_id, $field_name, $value );
    }
}

Here is a picture of the Custom field I have set: enter image description here

Upvotes: 2

Views: 1736

Answers (3)

silver
silver

Reputation: 5331

Then the best thing you can do is to save the value as json format, you can use maybe_serailize and maybe_unserialize to convert array to json and reverse.

First, you should assign a value on those checkbox that can be easily check/compared on with if state. Then pull the current value of that meta box and check if any of those checkboxes are already checked then you can assign them a checked attribute on load.

Here's an example code although UNTESTED. but this should give you an idea

Getting old values and assigning checked attributes on your input checkbox

$grips = get_post_meta( get_the_ID(), '_grips', true); // Pull the value of meta key
$grips = $grips ? maybe_unserialize( $grips ) : false; // unserialize its value

//Create a function to check if value exist on the array and to assign checked attribute on input checkbox
function _check_grips( $array, $key ) {
    if ( !$array || !is_array( $array ) ) return;
    return in_array($key, $array) ? ' checked': '';
}

Then your HTML should look like this

<form action="#" method="post">
    <div class="row">
        <div class="column left labels">
            <input type="checkbox" name="grips[]" value="1"<?php echo _check_grips('1'); ?>><label>Top (T1)</label>
            <input type="checkbox" name="grips[]" value="2"<?php echo _check_grips('2'); ?>><label>High (12)</label>
        </div>
        <div class="column left labels">
            <input type="checkbox" name="grips[]" value="3"<?php echo _check_grips('3'); ?>><label>Mid (23)</label>
            <input type="checkbox" name="grips[]" value="4"<?php echo _check_grips('4'); ?>><label>Low (34)</label>
        </div>
        <div class="column left labels">
            <input type="checkbox" name="grips[]" value="5"<?php echo _check_grips('5'); ?>><label>Bottom (P4)</label>
            <input type="checkbox" name="grips[]" value="6r"<?php echo _check_grips('6'); ?>><label>Other</label>
        </div>
    </div>
    <input type="submit" name="submit" value="Submit"/>
</form>

Then to save the value, just do something like below

$grips = maybe_serialize( $_POST['grips'] );
__update_post_meta( $the_post_id, '_grips', $grips);

Upvotes: 0

Levi Cole
Levi Cole

Reputation: 3684

I doubt the following will solve your issue completely but it might help narrow down the cause.

Some alterations I suggest making to your custom function...

<?php

function __update_post_meta( $post_id, $field_name, $value = '' ) {
    if ( empty( $value ) ) {
        delete_post_meta( $post_id, $field_name );
    } else {
        update_post_meta( $post_id, $field_name, $value );
    }
}

The middle step in your original question is redundant as WordPress takes care of this check with the update_post_meta function.

If the meta field for the post does not exist, it will be added and its ID returned.

Find out more here

And to answer your query "I imagine I need another function for arrays since this one only updates single custom fields".

No. This is not needed as the update_post_meta function serializes values automatically.

Lastly, the empty function checks for empty strings so no need for OR !$value.

The following values are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)

Find out more here

Upvotes: 0

Stem Step
Stem Step

Reputation: 71

I still want to learn how to do this with Meta data. But if you have ACF, the easy way to do this is via:

$field_key = "grips";
$value = $_POST['grips']; 
update_field( $field_key, $value, $the_post_id );
}

Upvotes: 1

Related Questions