Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it a way to get posts published between a date and today with query_posts() ?

Example : All posts published since 2012-04-01



How to add the filter date on this Query Posts ?

      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
Don't use query posts! :) –  Stephen Harris May 14 '12 at 13:21
Don't use query_posts(). Check this -> wordpress.stackexchange.com/a/1755/7890 –  moraleida May 14 '12 at 13:22
add comment

1 Answer

up vote 2 down vote accepted

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' );
share|improve this answer
Ok ! So the filter is now in $query_string. But how it works with my args in Query_Posts ? (Check my edit @Moraleida) –  Steffi May 14 '12 at 13:16
@Steffi - see updated answer. I hope you don't mind the addition, Moraleida. –  Stephen Harris May 14 '12 at 13:30
just added your current query, so you can ditch query_posts right away. :) And thanks @StephenHarris for the quick update! –  moraleida May 14 '12 at 13:32
Thank you @moraleida ! Amazing ! Just one thing. You said : "Avoid using query_posts." But it is better to use query_posts() in templates files (such as home.php) than new WP_Query(), no ? –  Steffi May 14 '12 at 13:43
Not really. query_posts should be used only to alter the main loop - and a lot of people argue that not even then (there's the pre_get_posts filter for that too). I often find myself using only WP_Query or get_posts for all my queries as they're standalone and can be used multiple times w/o interfering with anything else. Check the linked answers on your comments for a thorough explanation. :) –  moraleida May 14 '12 at 14:00
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.