拉取当前和未来帖子的日期查询

时间:2015-04-06 作者:Howdy_McGee

我想用date_query 查询包含今天日期或未来日期的帖子。想想看,我不想显示任何日期早于今天的帖子。我正在使用pre_get_posts 并按如下方式设置查询:

$query->set( \'post_status\', array( \'publish\', \'future\' ) );
$query->set( \'date_query\', array(
    array(
        \'year\'    => date( \'Y\' ),
        \'month\'   => date( \'m\' ),
        \'day\'     => date( \'d\' ),
        \'hour\'    => date( \'G\' ),
        \'minute\'  => date( \'i\' ),
        \'compare\' => \'>=\'
    )
) );

1 个回复
最合适的回答,由SO网友:birgire 整理而成

我想知道你是不是指这种date_query:

$query->set( \'date_query\', [
    [
        \'after\'     => \'today midnight\',
        \'column\'    => \'post_date_gmt\',
        \'inclusive\' => true,
    ],
] );
我们使用after 属性

这里是after 日期将由计算WP_Date_Query 作为:

gmdate( \'Y-m-d H:i:s\', strtotime( \'today midnight\', current_time( \'timestamp\' ) ) );
列设置为post_date_gmt, 它生成以下SQL条件:

wp_posts.post_date_gmt >= \'2015-04-06 00:00:00\'
2015年4月6日。

更新-一些测试:

我运行了一些测试,试图更好地理解WP_Date_Query:

wpse_test( 
    time(), 
    \'GMT\', 
    \'today\',
    \'Y-m-d H:i:s\',
    \'#1 Using time() for blog with UTC-7 and PHP timezone as GMT\'
);
wpse_test( 
    time(), 
    \'US/Pacific\', 
    \'today\',
    \'Y-m-d H:i:s\',
    \'#2 Using time() for blog with UTC-7 and PHP timezone as US/Pacific\'
);
wpse_test( 
    current_time( \'timestamp\' ) , 
    \'GMT\', 
    \'today\',
    \'Y-m-d H:i:s\',
    \'#3 Using current_time() for blog with UTC-7 and PHP timezone as GMT\'
);
wpse_test( 
    current_time( \'timestamp\' ) , 
    \'US/Pacific\', 
    \'today\',
    \'Y-m-d H:i:s\',
    \'#4 Using current_time() for blog with UTC-7 and PHP timezone as US/Pacific\'
);
其中:

/**
 * Test the string datetime generation in WP_Date_Query 
 * with a given timezone and current time
 */

function wpse_test( $time, $timezone, $timestring, $format, $title )
{
    date_default_timezone_set( $timezone );

    printf( \'--- %s ---\', $title );
    echo PHP_EOL;
    echo date_default_timezone_get();
    echo PHP_EOL;
    echo $time;
    echo PHP_EOL;
    echo date( $format, $time );
    echo PHP_EOL;
    echo gmdate( $format, $time );
    echo PHP_EOL;
    echo date( $format, strtotime( $timestring, $time ) );
    echo PHP_EOL;
    echo gmdate( $format, strtotime( $timestring, $time ) );
    echo PHP_EOL;
}
结果是以下输出:

--- #1 Using time() for blog with UTC-7 and PHP timezone as GMT ---
GMT
1428412051
2015-04-07 13:07:31
2015-04-07 13:07:31
2015-04-07 00:00:00
2015-04-07 00:00:00
--- #2 Using time() for blog with UTC-7 and PHP timezone as US/Pacific ---
US/Pacific
1428412051
2015-04-07 06:07:31
2015-04-07 13:07:31
2015-04-07 00:00:00
2015-04-07 07:00:00
--- #3 Using current_time() for blog with UTC-7 and PHP timezone as GMT ---
GMT
1428386851
2015-04-07 06:07:31
2015-04-07 06:07:31
2015-04-07 00:00:00
2015-04-07 00:00:00
--- #4 Using current_time() for blog with UTC-7 and PHP timezone as US/Pacific ---
US/Pacific
1428386851
2015-04-06 23:07:31
2015-04-07 06:07:31
2015-04-06 00:00:00
2015-04-06 07:00:00
我们可以看到time() 不随时区变化,以GMT为单位。

因此,似乎我们必须小心时区!

结束

相关推荐