因此,正如克里斯汀在评论中所建议的那样,我首先在寻找一种循环浏览现有帖子的方法。基于this answer, 我创建了一个基本插件,在激活时运行循环。
// Run the loop when the plugin is activated
register_activation_hook(__FILE__, \'update_my_metadata\');
function update_my_metadata(){
$args = array(
\'post_type\' => \'post\', // Only get the posts
\'post_status\' => \'publish\', // Only the posts that are published
\'posts_per_page\' => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
// Run a loop and update every meta data
$month=get_the_date(\'M\');
$year=get_the_date(\'Y\');
update_post_meta($post->ID, \'month-field\', $month); echo $month;
update_post_meta($post->ID, \'year-field\', $year); echo $year;
}
}
然而,当我检查post元字段时,月份和年份都只存储空字符串“”,而不是实际的日期值。
有什么建议吗?