比较发布日期与一个月前的#34;" unix时间

时间:2016-04-13 20:31:59

标签: wordpress unix-timestamp

我坚持一个问题,想到也许我错过了一些基本的东西。我在WordPress中创建了一个功能,用于列出发布日期少于一个月的帖子,现在"。我这样做是通过使用time()从当前时间减去2419200(30天)。然后我得到的帖子是> 30天前但是,我的函数错误地解释了这个范围,只提供了2周内的帖子!

除了这个问题之外,这个函数还有很多功能,所以我道歉,但是请遵循变量$ range,$ limit和$ publish_date来解决这个问题。请注意,出于测试目的,我将变量设置为"每月"手动开始。

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 个答案:

答案 0 :(得分:0)

您可以使用日期范围

运行查询,而不是遍布帖子
$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

此查询应返回上个月创建的帖子。

有关MDN Access Cotrol doc

的更多信息