简体   繁体   中英

Metabox in Wordpress not updating on Existing Posts

I created some custom metaboxes for my posts. The problem I'm having is that while the new metabox does show up in the dashboard for old posts, the default metadata I have in the metabox doesn't work until I update each post one by one. I am dealing with hundreds of posts, so I don't want to manually go to each post and update it. I've tried using a bulk action in the dashboard to update all the posts but that doesn't work.

Is there a way I can get existing posts to recognize the new metabox values without having to manually update each one?

Here is my code:

add_action( 'add_meta_boxes', 'cd_meta_box_add' );  
function cd_meta_box_add()  
{  
    add_meta_box( 'my-meta-box-id', 'Top of Post Options', 'cd_meta_box_cb', 'post', 'normal', 'high' );  
} 

function cd_meta_box_cb($post)  
{  
    // $post is already set, and contains an object: the WordPress post  
    global $post;  
    $values = get_post_custom( $post->ID );
    $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ''; 
    $selected2 = isset( $values['my_meta_box_select_2'] ) ? esc_attr( $values['my_meta_box_select_2'][0] ) : '';

    // We'll use this nonce field later on when saving.  
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' ); 
    ?> 

    <p> 
        <label for="my_meta_box_select">Home Page Featured Section</label> 
        <select name="my_meta_box_select" id="my_meta_box_select"> 
            <option value="featured_image" <?php selected( $selected, 'featured_image' ); ?>>Featured Image</option> 
            <option value="video" <?php selected( $selected, 'video' ); ?>>Featured Video</option> 
            <option value="none" <?php selected( $selected, 'none' ); ?>>None</option> 
        </select> 
    </p>

    <p>
        <label for="my_meta_box_select_2">Article Page Featured Section</label> 
        <select name="my_meta_box_select_2" id="my_meta_box_select_2">
            <option value="featured_image" <?php selected( $selected2, 'featured_image' ); ?>>Featured Image</option>
            <option value="article_image" <?php selected( $selected2, 'article_image' ); ?>>Article Image</option> 
            <option value="video" <?php selected( $selected2, 'video' ); ?>>Featured Video</option> 
            <option value="none" <?php selected( $selected2, 'none' ); ?>>None</option> 
        </select>
    </p>

    <?php      
}  

add_action( 'save_post', 'cd_meta_box_save' );  
function cd_meta_box_save( $post_id )  
{  
    // Bail if we're doing an auto save  
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn't there, or we can't verify it, bail 
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return; 

    // if our current user can't edit this post, bail  
    if( !current_user_can( 'edit_post' ) ) return;  

    // now we can actually save the data  
    $allowed = array(   
        'a' => array( // on allow a tags  
            'href' => array() // and those anchors can only have href attribute  
        )  
    );   

    if( isset( $_POST['my_meta_box_select'] ) )  
        update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) ); 

    if( isset( $_POST['my_meta_box_select_2'] ) )  
        update_post_meta( $post_id, 'my_meta_box_select_2', esc_attr( $_POST['my_meta_box_select_2'] ) );
} 

The easy solution for this is update_post_meta with WP_Query

This is how you can do it (Paste this code in your index.php page or any other template):

$loop = new WP_Query(array(
    'post_type' => 'post',
    'showposts' => -1,
));

while ($loop->have_posts()) : $loop->the_post();
    if (!get_post_meta($post->ID, 'my_meta_box_select', true))
        update_post_meta($post->ID, 'my_meta_box_select', 'featured_image');

    if (!get_post_meta($post->ID, 'my_meta_box_select2', true))
        update_post_meta($post->ID, 'my_meta_box_select2', 'featured_image');
endwhile;

Updates the post custom field my_meta_box_select and my_meta_box_select2 if doesn't contain any value will be set to default 'featured_image'

PS:
Execute this query once (Means visit page once where you inserted this code) and if you see the changes of posts having the default values than remove or comment the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM