Rajeev Vyas或多或少是正确的。。。就我个人而言,我认为最好的方法是制作一个小插件,当插件被激活时,它会更新你的所有日期。更新日期后。。。删除插件并记住,如果再次激活插件,所有日期都将再次更改。
因此,一步一步:
首先创建一个新的php文件并添加适当的标题信息,以便Wordpress将其识别为插件。将此保存到/wp-content/plugins文件夹。以下是一个示例:
/* Plugin Name: Plus One Year
* Description: Adds one year to the publish date of all published posts
* Author: Your Name
*/
现在,在这个新创建的文件中创建一个函数,并将其绑定到插件激活挂钩中。此功能将负责更新您的所有发布日期。
function plus_one_year_activate(){}
// Register Plugin Activation Hook
register_activation_hook(__FILE__, \'plus_one_year_activate\');
现在编写“plus\\u one\\u year\\u activate”功能:
function plus_one_year_activate(){
global $wpdb;
$format = \'Y-m-d H:i:s\'; // Format that wordpress uses to save dates
// Using $wpdb grab all of the posts that we want to update
$post_type = \'your_custom_post_type\'; //change this to your custom post type
$post_status = \'publish\'; // we only want to change published posts
$query = "SELECT ID, post_date, post_date_gmt FROM $wpdb->posts WHERE post_type = \'$post_type\' AND post_status = \'$post_status\'";
$posts = $wpdb->get_results( $query );
// This loop will handle the date changing using wp_update_post
foreach( $posts as $post ){
$id = $post->ID;
// get the old dates
$old_date = $post->post_date;
$old_gmt_date = $post->post_date_gmt;
// create the new dates and correctly format the new date before saving it to the database
$new_date = date( $format, strtotime( \'+1 year\' , strtotime( $old_date ) ) );
$new_date_gmt = date( $format, strtotime( \'+1 year\' , strtotime( $old_gmt_date ) ) );
$new_values = array (
\'ID\' => $id,
\'post_date\' => $new_date,
\'post_date_gmt\' => $new_date_gmt
);
wp_update_post( $new_values );
}
return;
}
保存并激活此插件。一旦激活。。。你所有的日期都会改变。激活后,请删除此插件,这样您就不会意外地在日期中再添加一年(即使删除该插件,您所做的更改也会保持不变)。希望这有帮助。