2016-04-13 25 views
0

我被困在一個問題上,並認爲也許我錯過了一些基本的東西。我在WordPress中創建了一個功能,列出發佈日期從「now」起不到一個月的帖子。我通過使用time()從當前時間減去2419200(30天)來做到這一點。然後我會獲得> 30天前的帖子。然而,我的功能錯誤地解釋了這個範圍,並且只提供了2周內的帖子!比較發佈日期到「一個月前」unix時間

有很多,除了這個問題,這個功能怎麼回事,所以我很抱歉,但跟着變量$範圍,$限制,並針對此問題$出版日期。請注意,出於測試目的,我在開始時手動將變量設置爲「每月」。

function send_archive() { 
$range = 'monthly'; 
$now = time(); 
switch ($range) { 
    case 'monthly' : 
     $limit = ($now - 2419200); // 30 days 
     break; 
    case 'weekly' : 
     $limit = ($now - 604800); 
     break; 
    case 'daily' : 
     $limit = ($now - 86400); 
     break; 
    } 

$post_types = get_post_types(); 
$posts = get_posts(array('post_type' => $post_types)); 
$all_users = get_users(); 
foreach ($all_users as $user) { 
    $excluded = excluded_admin($user->user_login); 
    echo 'excluded is: ' . $excluded . '<br />'; 
    echo 'user_login is: ' . $user->user_login . '<br />'; 
    if ($excluded != 'excluded') { 

     $frequency = get_user_meta($user->ID, 'iw_notify_frequency', true); 
     echo 'frequency is: ' . $frequency . '<br />'; 
     echo 'Range is: ' . $range . '<br />'; 
     echo 'Limit is: ' . date('D, d M Y H:i:s',$limit) . '<br />'; 
     $posts_in_range = ''; 
     $counter = 0; 
     $to = ''; 
     $subject = ''; 
     $headers = ''; 
     if ($frequency == $range) { 
      $posts_in_range = get_option('iw-notify-greeting'); 
      $posts_in_range .= '<p>' . strtoupper($range) . ' digest of posts from the City of Mukilteo</p>'; 
      foreach ($posts as $post) { 
       $published_date = strtotime($post->post_date_gmt); 

       $posts_in_range .= 'published_date: ' . $published_date. '<br />'; 
        $posts_in_range .= 'limit: ' . $limit . '<br />'; 
        $posts_in_range .= 'title: ' . $post->post_title . '<br />'; 
       if ($published_date > $limit) { 
        $match = has_user_selected_terms($user->ID, $post->ID); 
        if ($match != '') { 
         $headers = 'Content-type: text/html;charset=utf-8' . "\r\n"; 
         $posts_in_range .= $post->post_title; 
         $posts_in_range .= ' - published ' . date('M, d', $published_date) . '<br />'; 
         $posts_in_range .= $post->post_excerpt . '<br />'; 
         $posts_in_range .= get_permalink($post->ID); 
         $posts_in_range .= '<br /><br />'; 
         $counter++; 
        } 
       } 
      } 
     } 
     $posts_in_range .= get_option('iw-notify-unsubscribe') . '<br />----<br />'; 
     if ($counter != 0) { 
      $to = $user->user_email; 
      $subject = ucfirst($range) . ' archive from the City of Mukilteo'; 
      $headers = 'Content-type: text/html;charset=utf-8'; 
      $headers .= 'From: ' . get_option('from-name') . '<' . get_option('from-email') . '>' . "\r\n"; 
      $content = $posts_in_range; 
      wp_mail($to, $subject, $content, $headers); 
      echo "Email sent to " . $user->display_name . '<br />'; 
      echo $to . '<br />'; 
      echo $subject .'<br />'; 
      echo $headers . '<br />'; 
      echo $posts_in_range . '<br />'; 
      //echo $user->display_name . '<br />'; 
      //echo $posts_in_range; 
     } 
    } 
} 

}

回答

1

而不是去在你的帖子,你可以使用日期範圍

$args = array(
    'date_query' => array(
     array(
      'column' => 'post_date_gmt', 
      'after' => '1 month ago', 
     ) 
    ), 
    'posts_per_page' => -1, 
); 
$query = new WP_Query($args); 
//DO YOUR STUFF WITH THE RESULTS 

此查詢應該返回在上個月創造的後運行一個查詢。

有關更多信息Date Parameters

+0

哦哇。感謝那。很有用。 –