admin管理员组

文章数量:1430924

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
}

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
}

Share Improve this question asked Apr 17, 2019 at 7:47 EsmondEsmond 256 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: