Wordpress中的Metabox不会更新现有帖子

时间:2014-01-16 19:25:37

标签: php wordpress post

我为自己的帖子创建了一些自定义元变量。我遇到的问题是,虽然新的元数据确实显示在旧帖子的仪表板中,但我在元数据库中的默认元数据不会有效,直到我逐个更新每个帖子。我正在处理数百个帖子,因此我不想手动转到每个帖子并进行更新。我已尝试在信息中心中使用批量操作来更新所有帖子,但这不起作用。

有没有办法让现有的帖子能够识别新的元数据值,而无需手动更新每个元素?

这是我的代码:

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'] ) );
} 

1 个答案:

答案 0 :(得分:1)

这方面的简单解决方案是update_post_meta WP_Query

您可以这样做(在index.php页面或任何其他模板中粘贴此代码):

$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;

更新帖子自定义字段my_meta_box_selectmy_meta_box_select2如果不包含任何值,则会将其设置为默认'featured_image'

<强> P.S:
执行此查询一次(表示您在插入此代码时访问页面),如果您看到具有默认值的帖子更改,请删除或注释代码。