我爲我的帖子創建了一些自定義元框。我遇到的問題是,雖然新的metabox確實顯示在舊帖子的儀表板中,但我在metabox中的默認元數據不會工作,除非我逐一更新每個帖子。我正在處理數百個帖子,所以我不想手動轉到每個帖子並更新它。我試圖在儀表板中使用批量操作來更新所有帖子,但這不起作用。WordPress中的Metabox未在現有文章中更新
有沒有一種方法可以讓現有的帖子識別新的元組值,而無需手動更新每個元組?
這裏是我的代碼:
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']));
}
您的自定義metabox值是否顯示在批量編輯上?還需要爲舊帖子設置什麼默認值? –
不,它不顯示在批量編輯上。我需要默認爲'featured_image'。 – tcox