2017-04-15 51 views
0

我需要在WordPress中更改我所有博客帖子的最后修改日期和时间。到目前为止,我有....用WP查询修改WordPress发布的last_modified日期和时间

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); 
} 
} 

我没有得到任何错误或任何。我正在使用“回声”进行调试。我有两个问题。

  1. 我有6个职位,我只得到5
  2. 似乎没有更新数据库中发生的事情,最后修改后的字段

任何帮助,将不胜感激。

回答

1

您使用此代码。(它粘贴在你的主题function.php)

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(); 
    } 
} 
+0

工作!我只是将'init'改为'wp',它完成了这项工作!谢谢!!! – CDoc

+0

:D很棒,旅途愉快。 –