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
}