1

For a custom post type, I'm pulling in a list of another custom post types that I need to select for saving ...

<input type="checkbox" name="32"> My CPT <br>
<input type="checkbox" name="41"> My CPT 2 <br>
<input type="checkbox" name="42"> My CPT 3 <br>
<input type="checkbox" name="43"> My CPT 4

It's easy enough to save a single input, but how do I save multiple checkboxes?

update_post_meta( $post->ID, 'mycpt', $_POST['myinput'] );

2 Answers 2

3

You will need to save them as an array and currently your HTML is not in the correct format to do this.

<label for="my-cpt-32">
    <input type="checkbox" name="cpt_ids[]" value="32" id="my-cpt-32" />
    My CPT #32
</label>
<label for="my-cpt-41">
    <input type="checkbox" name="cpt_ids[]" value="41" id="my-cpt-41" />
    My CPT #41
</label>
<label for="my-cpt-43">
    <input type="checkbox" name="cpt_ids[]" value="42" id="my-cpt-43" />
    My CPT #43
</label>

When this get's $_POST'ed you will have an array of checked values, make sure to check it is 'set' e.g. isset( $_POST['cpt_ids'] ).

If you ticked 41 & 43 you would get array like this:

array(
    [0] => 41,
    [1] => 43
)

which you can use to save in your custom field, or a secondary table etc.

I hope this helps!

4
  • Awesome. I wasn't quite sure if HTML could essentially pass an array, but I supposed that's all done on the PHP side. In addition, how would you recommend then spitting this out on the page?
    – dcolumbus
    Commented Jul 24, 2012 at 20:51
  • When you have the IDs of the related posts, you can use a WP_Query and use the param post__in to do pretty much anything you want :)
    – Cristian
    Commented Jul 24, 2012 at 20:58
  • 1
    Thanks. When I try and use get_post_custom_values to then retrieve the saved data, I get the following: [0] => a:2:{i:0;s:2:"40";i:1;s:2:"32";} How do I "explode" this or use this?
    – dcolumbus
    Commented Jul 25, 2012 at 0:28
  • 1
    Ok, you are probably not using the last paramater of get_post_meta() which is a flag to get a single result or an array of results. No doubt you will only use this meta key once per post thus you can add the paramater like this get_post_meta( $post_id, 'my-meta-key', TRUE ); that way it will unserialize the array accordingly without you having to manually call the PHP function unserialize i hope that helps, and if you can accept the answer should it have helped :)
    – Cristian
    Commented Jul 25, 2012 at 4:58
1

The third parameter of update_post_meta() can be an array. Changing the name attribute of the checkbox to something like "myCPT[]" and adding a value attribute:

<input type="checkbox" name="myCPT[]" value="32"> My CPT <br>
<input type="checkbox" name="myCPT[]" value="41"> My CPT 2 <br>
<input type="checkbox" name="myCPT[]" value="42"> My CPT 3 <br>
<input type="checkbox" name="myCPT[]" value="43"> My CPT 4

will allow you to get all selected values as an array.

When the form is submitted, php will create an array of the values selected. The array can then be accessed using $_POST['myCPT'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.