Fetch posts from current week (Sunday to Saturday)

I’m trying to fetch posts from current week.

<?php 
    $args = array(
        'date_query' => array(
            array(
                'year' => date( 'Y' ),
                 'week' => date( 'W' ),
            ),
        ),
        'post_type' => 'stars', 'posts_per_page' => 99, 'order' => 'DEC',
    );
    $loop = new WP_Query( $args );
?>

This code is fetching the posts from Monday to Sunday (Start day of my week is “Monday” in general settings.)
But when I changed the start date of week to “Sunday” in the general settings, the above code is not fetching any posts.

I referred to this codex and it leads me to this MySQL function. But I’m not sure how to implement that in my query.

Any ideas?

2 Answers
2

You can try:

'date_query' => array(
    array(
        'year' => date( 'Y' ),
        'week' => strftime( '%U' ),
    ),
 ),

where %U is:

Week number of the given year, starting with the first Sunday as the
first week

or

'date_query' => array(
    array(
        'after' => strtotime( 'last Sunday' ),
    ),
 ),

Leave a Comment