Title: WordPress custom post type and checkboxes
Last modified: August 19, 2016

---

# WordPress custom post type and checkboxes

 *  [Jess](https://wordpress.org/support/users/jessn/)
 * (@jessn)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/wordpress-custom-post-type-and-checkboxes/)
 * Hi! I’m trying to add a checkbox as a field in a custom post type but I can’t
   get WordPress to save the value of checked or unchecked. I don’t have the foggiest
   idea of how to make it work and I was hoping one of you guys could point me in
   the right direction!
 * Inside my function to create the checkbox I have:
    `$checkbox = $custom["checkbox"][
   0];`
 * Which works fine. And for the input field I have:
 *     ```
       <?php if($checkbox == "true") $checkbox = 'checked="checked"';?>
       <input type="checkbox" id="checkbox" name="checkbox"   value="<?php echo (isset($checkbox) && $checkbox != ''  ? 'checked' : ''); ?>"  />
       ```
   
 * The if/else stuff was something I was trying out from a different forum topic.
   I don’t know if it should be there or not, but it doesn’t work even without it.
   The checkbox shows up, just doesn’t save.
 * Then for the update function I have:`
    update_post_meta($post->ID, “checkbox”,
   $_POST[“checkbox”]);`
 * Any help would be greatly appreciated!! Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [Jess](https://wordpress.org/support/users/jessn/)
 * (@jessn)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/wordpress-custom-post-type-and-checkboxes/#post-1581786)
 * Well I figured it out. Kind of. Using radio buttons instead of checkboxes, which
   actually works better for my purposes. If it helps anyone here is what I did:
 *     ```
       <input type="radio" id="something" name="something"  value="1" <?php
       if ($something == 1) echo 'checked';?>/>
       <input type="radio" id="something" name="something"  value="2" <?php
       if ($something == 2) echo 'checked';?>/>
       <input type="radio" id="something" name="something"  value="3" <?php
       if ($something == 3) echo 'checked';?>/>
       ```
   
 * Then just the basic update code:
 * `update_post_meta($post->ID, "something", $_POST["something"]);`
 *  [Austin Ginder](https://wordpress.org/support/users/austinginder/)
 * (@austinginder)
 * [15 years ago](https://wordpress.org/support/topic/wordpress-custom-post-type-and-checkboxes/#post-1582130)
 * I had the same problem. I ended up getting it to work doing something messy like
   this:
 *     ```
       if($_POST["checkbox"] == "on") {
       	$checkbox = "on";
         } else {
       	$checkbox = "off";
         }
         update_post_meta($post->ID, "checkbox", $checkbox);
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WordPress custom post type and checkboxes’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Austin Ginder](https://wordpress.org/support/users/austinginder/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/wordpress-custom-post-type-and-checkboxes/#post-1582130)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
