显示当前和未来的帖子

时间:2013-01-30 作者:Rob

我正在使用以下代码显示带有今天日期或将来日期的帖子。。。除了未来的部分不起作用。如果我去掉If语句,那么它会显示所有帖子——过去、现在和未来。

$today = date( \'d M\' ); 
$pages = get_children( array(
    \'post_status\' => \'future,publish\',
    \'post_type\' => \'event\',
    \'sort_order\' => \'DESC\'
) );
foreach ( $pages as $post ) {
    setup_postdata($post);
    $item = get_the_date(); 
    if ($item >= $today) { 
        ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
    }
}
wp_reset_query();
如何显示包含今天日期的帖子和将来的帖子?

3 个回复
SO网友:david.binda

使用get_posts function 具有WP_Query params 而不是get\\u children。代码将如下所示:

$today = getdate(); 
$pages = get_posts( array(
    \'post_status\' => array( \'publish\', \'future\' ),
    \'post_type\' => \'event\',
    \'sort_order\' => \'ASC\',
    \'orderby\' => \'date\',
    \'year\' => $today["year"],
    \'monthnum\' => $today["mon"],
    \'day\' => $today["mday"]
) );
foreach ( $pages as $post ) {
    setup_postdata($post);
        ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
}

wp_reset_postdata();

SO网友:Eugene Manuilov

posts_where 筛选条件并将其添加到sql查询:

function wpse8170_filter_where( $where = \'\' ) {
    $where .= sprintf( " AND post_date >= \'\'", date( \'Y-m-d\' ) );
    return $where;
}

// add your hook
add_filter( \'posts_where\', \'wpse8170_filter_where\' );

// get pages
$pages = get_children( array(
    \'post_status\' => \'future,publish\',
    \'post_type\' => \'event\',
    \'sort_order\' => \'DESC\'
) );

// remove your hook to not have influence on other get posts queries
remove_filter( \'posts_where\', \'wpse8170_filter_where\' );

foreach ( $pages as $post ) {
    setup_postdata($post);

    ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
}
wp_reset_query();

SO网友:Rob

我知道它在做什么。。。

它是用一个月的哪一天来衡量的,所以我把它改成Ymd.

结束