More than 1 Year Date Query

时间:2017-11-06 作者:Dev

是否可以对这样的日期查询使用多年-

$args = array(
    \'posts_per_page\' => \'-1\',
    \'date_query\' => array(
        array(
            \'year\'  => array( 2016, 2017 )
        ),
    ),
);


$posts = new WP_Query(array( $args ) );
我试过了,但没用

\'year\'  => array( 2016, 2017 )

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

您可以使用beforeafter, 看见date arguments.

$args = array(
    \'posts_per_page\' => \'-1\',
    \'date_query\'     => array(
        array(
            \'after\'  => \'December 31st, 2015\', // i.e., 2016+.
            \'before\' => \'January 1st, 2018\', // i.e., before 2018.
        ),
    ),
);
$query = new WP_Query( $args );
基于article 由日期查询提交者之一编写,看起来您可以使用AND OR 逻辑日期查询。所以你也可以这样做。

$args = array(
    \'posts_per_page\'  => \'-1\',
    \'date_query\'      => array(
        \'relation\'    => \'OR\',
        array( \'year\' => 2016 ),
        array( \'year\' => 2017 ),
    ),
);
$query = new WP_Query( $args );
根据作者的说法,所有可能的论点。

\'date_query\' => array(
    \'column\' => \'optional, column to query against, default is post_date\',
    \'compare\' => \'optional, see WP_Date_Query::get_compare()\',
    \'relation\' => \'optional, OR or AND, how the sub-arrays should be compared, default is AND\',
    array(
        \'column\' => \'see above\',
        \'compare\' => \'see above\',
        \'after\' => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
        \'before\' => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
        \'inclusive\' => \'boolean, for after/before, whether exact value should be matched or not\',
        \'year\' => \'4 digit int\',
        \'month\' => \'int, 1-12\',
        \'week\' => \'int, 0-53\',
        \'day\' => \'int, 1-31\',
        \'hour\' => \'int, 0-23\',
        \'minute\' => \'int, 0-60\',
        \'second\' => \'int, 0-60\',
    ),
    array(
        ...
    ),
    ..
),

SO网友:rvictorcardozo

在我的情况下,我使用AfterBefore 以显示两年之间最受欢迎的帖子。

工作很好!

$current_year = get_the_date(\'Y\'); //Get Current Year of the Page - Header Page
$last_year = (get_the_date(\'Y\') - 2) //2 is the year for calculated the period ;    
$args = array(
          \'post_type\' => \'post\',
          \'posts_per_page\'  => 6,
          \'meta_key\' => \'post_views_count\',
          \'orderby\' => \'meta_value_num\',
          \'order\' => \'RAND\',
          \'date_query\'     => array(
            array(
                \'after\'  => $last_year,
                \'before\' => $current_year,
            ),
          ),
          \'post_status\'  => \'publish\'
        );
$myposts = new WP_Query( $args );

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post