如何让帖子在某个日期和今天之间发布?

时间:2012-05-14 作者:Steffi

这是一种在日期和今天之间发布帖子的方式吗query_posts() ?

示例:自发布以来的所有帖子2012-04-01

谢谢

编辑:

如何在此查询帖子上添加筛选日期?

query_posts( array(  
    array(\'post\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_format\',
            \'field\' => \'slug\',
            \'terms\' => array(\'post-format-image\')
        )
    ),
    \'cat\' => \'-173\',
    \'post_status\' => \'publish\'
) );

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

UPDATE December 23 2014

有一种更好的方法date_query 的属性WP_Query 类别:

$args = array(
    \'post_type\' => \'post\', 
    \'tax_query\' => array(
        array( 
            \'taxonomy\'  => \'post_format\',
            \'field\'     => \'slug\',
            \'terms\'     => array( \'post-format-image\' )
        )
    ),
    \'cat\'           => \'-173\',
    \'post_status\'   => \'publish\',
    \'date_query\'    => array(
        \'column\'  => \'post_date\',
        \'after\'   => \'- 30 days\'
    )
);
$query = new WP_Query( $args );

OLD ANSWER

使用Time Parameters in WP_Query()

引用法典中的示例:

Return posts from the last 30 days:

// This takes your current query, that will have the filtering part added to.
$query_string = array(
    \'post_type\' => \'post\', 
    \'tax_query\' => array(
        array(
            \'taxonomy\'  => \'post_format\',
            \'field\'     => \'slug\',
            \'terms\'     => array( \'post-format-image\' )
        )
    ),
    \'cat\'           => \'-173\',
    \'post_status\'   => \'publish\'
);

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = \'\' ) {
    // posts in the last 30 days
    $where .= " AND post_date > \'" . date( \'Y-m-d\', strtotime( \'-30 days\' ) ) . "\'";
    return $where;
}

add_filter( \'posts_where\', \'filter_where\' );
$query = new WP_Query( $query_string );
remove_filter( \'posts_where\', \'filter_where\' );
<小时>Edit(回答OP的最新问题)。

Avoid using query_posts. 您可以使用上述技术来更改主查询(需要一些额外的conditionals - 是主页,是名为“foobar”等的页面):

function wpse52070_filter_where( $where = \'\' , $query ) {
   if( $query->is_main_query() && is_page( \'foobar\' ) ){
      // posts in the last 30 days
      $where .= " AND post_date > \'" . date( \'Y-m-d\', strtotime( \'-30 days\' ) ) . "\'";
   }

    return $where;
}
add_filter( \'posts_where\', \'wpse52070_filter_where\' );

SO网友:pingle60

如果希望在两个日期之间获取帖子,请使用date\\u查询参数中的before和after参数,

$query_string = array(
  \'post_type\' => \'post\', 
  \'date_query\' => array(
    \'column\' => \'post_date\',
    \'after\' => \'2012-04-01\',
    \'before\' => \'2012-04-30\' 
  ),
  \'tax_query\' => array(
      array( 
         \'taxonomy\' => \'post_format\',
         \'field\' => \'slug\',
         \'terms\' => array(\'post-format-image\')
      )
  ),
  \'cat\' => \'-173\',
  \'post_status\' => \'publish\'
);

SO网友:Kode

从3.7开始,您可以使用date\\u查询http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

因此,传递的参数如下所示:

$query_string = array(
      \'post_type\' => \'post\', 
      \'date_query\' => array(
        \'after\' => \'2012-04-01\' 
      ),
      \'tax_query\' => array(
          array( 
             \'taxonomy\' => \'post_format\',
             \'field\' => \'slug\',
             \'terms\' => array(\'post-format-image\')
          )
      ),
      \'cat\' => \'-173\',
      \'post_status\' => \'publish\'
);

结束