admin管理员组文章数量:1426666
Is it a way to get posts published between a date and today with query_posts()
?
Example : All posts published since 2012-04-01
Thanks
EDIT :
How to add the filter date on this Query Posts ?
query_posts( array(
array('post'),
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
'cat' => '-173',
'post_status' => 'publish'
) );
Is it a way to get posts published between a date and today with query_posts()
?
Example : All posts published since 2012-04-01
Thanks
EDIT :
How to add the filter date on this Query Posts ?
query_posts( array(
array('post'),
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-image')
)
),
'cat' => '-173',
'post_status' => 'publish'
) );
Share
Improve this question
edited Jun 17, 2018 at 20:02
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked May 14, 2012 at 12:59
SteffiSteffi
7653 gold badges12 silver badges20 bronze badges
2
- Don't use query posts! :) – Stephen Harris Commented May 14, 2012 at 13:21
- Don't use query_posts(). Check this -> wordpress.stackexchange/a/1755/7890 – moraleida Commented May 14, 2012 at 13:22
3 Answers
Reset to default 36UPDATE December 23 2014
There is a better method using date_query
property of WP_Query
class:
$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
Use the Time Parameters in WP_Query()
Quoting example from the Codex:
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 (in response to the OP's updated question).
Avoid using query_posts. You can use the above technique to alter your main query (subject to some extra conditionals - is home page, is a page called 'foobar' etc. ):
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' );
If you wish to get posts between two dates, then use the before and after parameters in the date_query parameter,
$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'
);
As of 3.7 you can use date_query http://codex.wordpress/Class_Reference/WP_Query#Date_Parameters
So the args passed would look like:
$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'
);
本文标签: loopHow to get posts published between a date and today
版权声明:本文标题:loop - How to get posts published between a date and today? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745462069a2659368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论