admin管理员组文章数量:1391960
Is it possible to use multiple years with a date query like this -
$args = array(
'posts_per_page' => '-1',
'date_query' => array(
array(
'year' => array( 2016, 2017 )
),
),
);
$posts = new WP_Query(array( $args ) );
I tried this but it doesn't work
'year' => array( 2016, 2017 )
Is it possible to use multiple years with a date query like this -
$args = array(
'posts_per_page' => '-1',
'date_query' => array(
array(
'year' => array( 2016, 2017 )
),
),
);
$posts = new WP_Query(array( $args ) );
I tried this but it doesn't work
'year' => array( 2016, 2017 )
Share
Improve this question
asked Nov 6, 2017 at 12:46
Brad DaltonBrad Dalton
6,9852 gold badges36 silver badges47 bronze badges
2 Answers
Reset to default 3You can use before
and after
, see 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 );
Based on an article written by one of the date query committers, it looks like you can use AND
OR
logic date queries too. So you can also do this.
$args = array(
'posts_per_page' => '-1',
'date_query' => array(
'relation' => 'OR',
array( 'year' => 2016 ),
array( 'year' => 2017 ),
),
);
$query = new WP_Query( $args );
All possible arguments according to that author.
'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(
...
),
..
),
In my case, I use After
and Before
to show the most popular post in the period between two years.
Work's fine!
$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 );
本文标签: More than 1 Year Date Query
版权声明:本文标题:More than 1 Year Date Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704092a2620735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论