我需要将所有自定义帖子类型的发布年份从2010年更改为2011年。
最快的方法是什么?我有200个条目,所以手动操作需要几个小时!!
我需要将所有自定义帖子类型的发布年份从2010年更改为2011年。
最快的方法是什么?我有200个条目,所以手动操作需要几个小时!!
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;
}
保存并激活此插件。一旦激活。。。你所有的日期都会改变。激活后,请删除此插件,这样您就不会意外地在日期中再添加一年(即使删除该插件,您所做的更改也会保持不变)。希望这有帮助。可能您必须在插件文件中编写代码并运行。。。
使用get\\u posts获取自定义帖子类型的帖子,然后循环它。内部循环使用wp\\u update\\u post函数更新post\\u日期。
这里是wp\\u update\\u post的参考http://codex.wordpress.org/Function_Reference/wp_update_post
$my_post = array();
$my_post[\'ID\'] = 37;
$my_post[\'post_date\'] = your updated date
// Update the post into the database
wp_update_post( $my_post );
您是否考虑过在数据库中执行此操作(例如使用phpmyadmin)?
我认为这是最奇怪的文字印刷问题。一个拥有大量帖子数据库的客户网站已经有了自己的想法。前一分钟一切似乎都很好,接下来的帖子、类别和标签都不见了。此时仪表板告诉我9个类别中有2309个帖子,但当我单击admin中的categories链接时,列表中没有任何帖子。我可以在admin中看到所有帖子,但尽管它们过去被分配到类别并列出标签,但现在它们都“未分类”,没有标签。很明显,wordpress数据库在某种程度上已经损坏,但从哪里开始尝试修复呢!?以下是我迄今为止测试的内容:插件–我关闭了所有当前的插件。主题–我