2

I have two radio buttons as custom metabox fields for pages/posts. One radio button has the value of "enable" and the other has the value of "disable". My radio buttons will stay checked only if I manually got to the page/post to check them and then update the page/post. If I change the post meta value for that field through a post meta query the different radio option does not show being checked.

The query I am running is this and it works. I checked the values in phpmyadmin. On the edit screen the metabox still does not show the correct radio button being checked.

function my_action() {
    global $wpdb; 

      $args = array(
        'post_type' => array('post','page'), 
        'post_status' => 'publish', 
        'posts_per_page'   => -1 // Get every post
    );
    $posts = get_posts($args);
    foreach ( $posts as $post ) {
        // Run a loop and update every meta data
        update_post_meta( $post->ID, 'vmo_code_radio_value
', 'enable' );
    }
wp_die(); // this is required to terminate immediately and return a proper response
}

Here is the content callback.

function vmo_code_enable_html($post)
{

  $meta = get_post_meta($post->ID);
$vmo_code_radio_value = ( isset( $meta['vmo_code_radio_value'][0] ) && '' !== $meta['vmo_code_radio_value'][0] ) ? $meta['vmo_code_radio_value'][0] : '';

  wp_nonce_field('vmo_code_control_meta_box', 'vmo_code_control_meta_box_nonce'); // Always add nonce to your meta boxes!
  ?>
<style type="text/css">
.post_meta_extras p {
    margin: 20px;
}

.post_meta_extras label {
    display: block;
    margin-bottom: 10px;
}
</style>
<div class="post_meta_extras">
    <p>    
    <label>Enable<input type="radio" name="vmo_code_radio_value" value="enable" <?php checked(  'enable' ,$vmo_code_radio_value  ); ?>></label>
    <label>Disable<input type="radio" name="vmo_code_radio_value" value="disable" <?php checked(  'disable' ,$vmo_code_radio_value  ); ?> ></label>
    </p>
    <?php
}

1 Answer 1

0

So your checkbox code works fine. But the problem is.. the meta key.

If you do var_dump( $meta['vmo_code_radio_value'] );, you'd get a NULL because the meta key contains a newline and it will be saved as is:

update_post_meta( $post->ID, 'vmo_code_radio_value
', 'enable' );

But if you do:

var_dump( $meta['vmo_code_radio_value
'] ); // note the newline

Then you'd see the proper value: enable.

So the fix is simple: remove the newline. I.e.

update_post_meta( $post->ID, 'vmo_code_radio_value', 'enable' );

And by the way, to retrieve a single value of a metadata, you could use get_post_meta( <post ID>, 'key', true ) like so in your case:

$vmo_code_radio_value = get_post_meta( $post->ID, 'vmo_code_radio_value', true );
1
  • 1
    Turns out the query was making another meta_id instead of just replacing the already existing one. php // old code update_post_meta( $post->ID, 'vmo_code_radio_value This is the new code below which got everything to work. php // new code $old_meta = get_post_meta( $post->ID, 'vmo_code_radio_value', true); $old_meta['enable'] = $new_value; update_post_meta( $post->ID, 'vmo_code_radio_value', 'enable' );
    – Esmond
    Commented Apr 17, 2019 at 21:16

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.