将 post 发布日期与 "one month ago" unix 时间进行比较

Comparing post publish date to "one month ago" unix time

我被一个问题困住了,我想我可能遗漏了一些基本的东西。我正在 WordPress 中创建一个函数,列出发布日期从 "now" 开始不到一个月的帖子。我使用 time() 从当前时间减去 2419200(30 天)。然后我得到超过 30 天前的帖子。但是,我的函数错误地解释了这个范围,并且只提供 2 周范围内的帖子!

除了这个问题之外,这个函数还有很多事情要做,所以我很抱歉,但是对于这个问题,请遵循变量$range、$limit和$publish_date。请注意,出于测试目的,我在开始时手动将变量设置为 "monthly"。

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

}

您可以 运行 使用日期范围

查询,而不是遍历 post
$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

此查询应该return post 上个月创建的。

有关Date Parameters

的更多信息