How to Get This Week’s Posts

WordPress introduces date_query since 3.7 which makes it much easier for developers who have to retrieves posts in a certain range.

function get_this_week_posts(){

    $date_query = array(
                            'after'=>'1 week ago'
    $args = array(
                    'post_type' => 'post',
                    'date_query' => $date_query,
                    'no_found_rows' => true,
                    'suppress_filters' => true,

    $query = new WP_Query( $args );

    return $query->posts;

As you can see, it’s very amazing that you can simply use “1 week ago” as parameter. If you look into the source code, you’ll find out it’s actually taking advantage of strtotime which will “parse about any English textual datetime description into a Unix timestamp”.

You can also combine tax_query to specific a category.

If you are curious about the other parameters of WP_Query, you may want to take a look at this article.

Share and Enjoy

    FacebookTwitterGoogle PlusLinkedInStumbleUponPinterestRedditTumblrDiggEmailPrint

Related Posts