Php 更改Wordpress帖子';上次使用WP查询修改日期和时间
我需要更改WordPress中所有博客帖子的最后修改日期和时间。到目前为止我已经Php 更改Wordpress帖子';上次使用WP查询修改日期和时间,php,wordpress,Php,Wordpress,我需要更改WordPress中所有博客帖子的最后修改日期和时间。到目前为止我已经 add_action( 'wp', 'asd' ); function asd() { $post_list = get_posts( array( 'post_per_page' => '-1' ) ); foreach ( $post_list as $post ) { // $posts[] += $post->ID; $postID = $
add_action( 'wp', 'asd' );
function asd()
{
$post_list = get_posts( array(
'post_per_page' => '-1'
) );
foreach ( $post_list as $post ) {
// $posts[] += $post->ID;
$postID = $post->ID;
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
echo $postID . ' ||| ' . $datetime . '<br>';
global $wpdb;
$wpdb->query( "UPDATE `$wpdb->posts` SET `post_modified` = '" . $datetime . "' WHERE ID = " . $postID);
}
}
add_action('wp','asd');
函数asd()
{
$post\u list=get\u posts(数组(
“每页发布”=>“-1”
) );
foreach($post_列表为$post){
//$posts[]+=$post->ID;
$postID=$post->ID;
$datetime=日期('Y-m-DH:i:s',当前时间('timestamp',0));
echo$posted.“| |”.$datetime.“
”;
全球$wpdb;
$wpdb->query(“更新`$wpdb->posts`SET`post\u modified`='”$datetime.“,其中ID=”..$postID);
}
}
我没有犯任何错误。我使用“echo”进行调试。我有两个问题
add_action('init','change_mypost_date');
功能更改\u mypost\u日期(){
$args=数组(
“post_type”=>“属性列表”,
“每页帖子”=>-1
);
$thew_query=newwp_query($args);
if($the\u query->have\u posts()){
回声“”;
while($the\u query->have\u posts()){
$the_query->the_post();
$datetime=日期('Y-m-DH:i:s',当前时间('timestamp',0));
$current\u post=数组(
'ID'=>获取\u ID(),
“post_modified”=>$datetime
);
//将帖子更新到数据库中
wp_更新_职位(当前职位);
}
wp_reset_postdata();
}
}
您可以使用此代码。(将其粘贴到主题函数.php中)
add_action('init','change_mypost_date');
功能更改\u mypost\u日期(){
$args=数组(
“post_type”=>“属性列表”,
“每页帖子”=>-1
);
$thew_query=newwp_query($args);
if($the\u query->have\u posts()){
回声“”;
while($the\u query->have\u posts()){
$the_query->the_post();
$datetime=日期('Y-m-DH:i:s',当前时间('timestamp',0));
$current\u post=数组(
'ID'=>获取\u ID(),
“post_modified”=>$datetime
);
//将帖子更新到数据库中
wp_更新_职位(当前职位);
}
wp_reset_postdata();
}
}
工作正常!!!我刚把“init”改为“wp”,它成功了!谢谢您!!!:I’旅途愉快,工作愉快!!!我刚把“init”改为“wp”,它成功了!谢谢您!!!:I’旅途愉快。
add_action('init','change_mypost_date');
function change_mypost_date(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$datetime = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
$current_post = array(
'ID' => get_the_ID(),
'post_modified' => $datetime
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}