Search

How to Query WordPress Posts or Comments by Date or Time

    Bharat Pareek
Listen to this article

Retrieving posts based on a particular date or time duration is a common use case during development. To retrieve the posts or comments by date and time, you need to use the date query. The date query provides several parameters which allows you to use a time and date as conditional arguments while fetching posts.

Date Query WordPress

 

Fetching Posts with Date Query

To fetch posts conditionally, we need to use the WP_Query function with the specified arguments. To get posts bound by a date or time limit, we need to specify a conditional date query. For example, to get all posts, published in the last week, you would have to frame your date query as follows:

$last_week_posts = new WP_Query( array('date_query' => array(
                     array (
                       'after' => '1 week ago',)
                           )
                       ));

To get posts after 1st January 2014, and before 1st March 2014 your date query would be:

'date_query' => array( array (
                  'year' => 2014,
                  'day' => 1,
                      array('month' => array(1, 6 ),
                      'compare' => 'BETWEEN',
                   ),
                ));

There are several conditions you can use to frame your query. You can use, year, month, day, time till seconds, etc. For additional details read, Date Parameters in WordPress Queries.

 

Fetching Comments with the Date Query

You can also use the date query to get the comments according to a time duration. As an example, say you had to get the comments which were not older than 30 days. You can use the get_comments function to get the comments, and use the data_query as one of the arguments for the function.

Thus to get recent comments (approved comments, posted at the most 30 days ago) , you have to do the following:

//set the arguments
$args = array(
         'orderby' => 'date',
         'status' => 'approve',
         'order' => 'DESC',
         'date_query' => array(
                array(
                'after' => '30 days ago',
                  )
               )
            );
// get the comments using the arguments
$comments = get_comments($args);

[space]

Date queries are a very important and useful feature which allows you to retrieve posts and comments based on a date and time parameter.

 

Picture of Bharat Pareek

Bharat Pareek

8 Responses

  1. Is there a way to filter or limit dynamically? For example if we upload 100 posts and have a field that we put the beginning date into and the ending date into and then make that post only show up between those dates?

  2. Hi Andrew,

    You can easily filter the posts or comments the way you want, WordPress date queries are quite flexible, check the code given below for your particular requirement. 🙂

    <?php
    // Get all posts from August 1st to September 30th, inclusive
    // You can use strtotime() - compatible strings also, example given below
    $filtered_posts = new WP_Query( array(
        'date_query' => array(
            array(
                // String via strtotime()
                'after'     => 'August 1st, 2014',
                // Or if you want, an array
                'before'    => array(
                    'year'  => 2014,
                    'month' => 9,
                    'day'   => 30,
                ),
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    ) );
    ?>
    

    Note: ‘inclusive’ => false will exclude August 1st & September 30th.

  3. How can I display post for a date selected from datepicker? Means I want to display only single post which is posted on specific date. When User come to the site and select a date from date picker then display those post which is published on that date.
    And I also want to know that How can I achive this functionality in wordpress?
    Please reply me ASAP.

    1. Hi Adarsh,
      I’m guessing you have an input field with a date picker. You have to send the date picked to the backend using AJAX and then fire a date query to get the posts published on the picked date.

  4. Bharat I am passing it through hidden field. but I dont know how to pass that date to get post?
    Will you please put code snippets if you have or any reference link which helpfull to me

  5. Hello, is there a way to show last years posts today? I have a client who has been blogging for a few years, he wants to display posts that were made a year or 2 ago on the same date. Is there a way to do this automatically? Without having to do it manually coded everyday?

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads