Here's my problem, I allow a certain user type to create custom post type in the front-end of my site, with the wp_insert_post() function... users can put values within a form and there's couple of input types, checkboxes, ranges, numbers, and text fields.
But I have a problem with checkbox and radio inputs. when users save values, they don't appear in the post in the admin panel, I think the problem is in the update_post_meta() condition...
here how was the condition:
     if( isset( $_POST[ 'checkbox_1' ] ) ) {
        update_post_meta( $post_id, 'checkbox_1', 'yes' );
        update_post_meta( $post_id, 'checkbox_1', '' ); 

so I changed it like that:

$stored_meta= get_post_meta($post_id);

     if(isset( $_POST[ 'checkbox_1' ] ) ) {
        update_post_meta( $post_id, 'checkbox_1', $_POST[ 'checkbox_1' ]) ;
        update_post_meta( $post_id, 'checkbox_1', $stored_meta['checkbox_1'][0]); 

Now they appear, but it's impossible to modify them in the admin panel, for example if user checked an input, even if I unchek it, it remains check after update.

I have tried other logic, but they all failed.

here's how look my checkbox input:

<input type="checkbox" name="checkbox_1" value='yes' <?php if ( isset ( $stored_meta['checkbox_1'][0] ) ) checked( $stored_meta['checkbox_1'][0], 'yes'); ?>>
  • Well, by updating it to what's in $stored_meta, it looks like you're making it so if it's unchecked, it won't change, while if it is checked, it will change. Your first one really does look right... What if you change the $stored_meta['checkbox_1'][0] to 'no'?
    – Stevish
    Sep 1, 2021 at 18:49
  • Checkboxes always throw me for a loop eventually because there's no negative to them. They're either there in the $_POST var with the yes value, or entirely missing. I often end up using 2 radios for a yes/no response instead, with one automatically selected.
    – Stevish
    Sep 1, 2021 at 18:53
  • so I have to make 2 inputs for every checkbox? and for one of those, put no as value, and set it as default? isn't it?
    – T3mmX
    Sep 1, 2021 at 19:04
  • I have tried, it doesn't work, it still does not display the values ​​passed in front-end. When I get a var_dump(get_post_meta($post-ID)) in the admin side, [checkbox_1][0] = 'no' . however it is a 'yes' which was passed. it's like the post is updated before modification.
    – T3mmX
    Sep 1, 2021 at 20:45
  • I use your radio trick instead and it works... Thanks. so all I have to do now is to create a toggle switch.
    – T3mmX
    Sep 1, 2021 at 21:16

1 Answer 1


Welcome to WordPress SO.

You may already have solved the issue as suggested by @Stevish, but just in order to avoid promoting the idea of checkboxes somehow being bad or difficult, I'd like to point out, that they're perfectly okay and logical. :)

What is the problem in your first snippet, is the additional closing square bracket ']' in the value, which would cause a syntax error and thus PHP Fatal error.

Change this:

update_post_meta( $post_id, 'checkbox_1', 'yes'] );

to this:

update_post_meta( $post_id, 'checkbox_1', 'yes' );

Also, a handy way to deal with single value metadata is to specify the $key and the $single parameter like this:

$value = get_post_meta( $post_id, 'checkbox_1', true );

That way you get the single value, and not an array where your value is the [0]. That way you can skip many if( isset( $meta['whatever'][0] ) ) kind of checks. :)

  • Oh man! I looked over and over at his code and could NOT find any errors! Good eye!
    – Stevish
    Sep 2, 2021 at 13:32
  • That happens :) A good IDE will catch most of those.
    – jgangso
    Sep 2, 2021 at 13:36
  • this error wasn't in my code actually. As I said I tried many logic, and the first one wasn't no longer in my code, for the purpose of this post, I modified the code here to meet my explanation, and I guess made the error here... sorry!
    – T3mmX
    Sep 3, 2021 at 17:47
  • thanks for the clue about the single parameter, I used it, but completely forgot it at this point
    – T3mmX
    Sep 3, 2021 at 17:48

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.