1

With the date_query in wordpress I was trying to get posts for the last working week (Monday to Friday)

I was using:

$base_array        = array(
    'posts_per_page'    => -1,
    'fields'            => 'ids',
    'post_type'         => 'cpt',
    'post_status'       => array('publish'),
    'date_query'        => array(
            'before'    => 'next Saturday',
            'after'     => 'last Monday'
    )
);

 $base = get_posts($base_array);

But today, a Thursday, returns these values:

From 31/10/2016 to 28/10/2016

Which makes sense with the 'last' days but is there any way that I can get the last Monday to last Friday? Regardless on the day the WP_Query is queried.

2

1 Answer 1

3

Well, try the below code-

$base_array        = array(
    'posts_per_page'    => -1,
    'fields'            => 'ids',
    'post_type'         => 'cpt',
    'post_status'       => array('publish'),
    'date_query'        => array(
            'after'     => strtotime( 'previous week Monday' ),
            'before'    => strtotime( 'previous week Friday' )
    )
);

$base = get_posts($base_array);

I've not tested it. But I tested that below code returns the perfect date-

date('Y-m-d',strtotime('previous week Monday'));
date('Y-m-d',strtotime('previous week Friday'));

It returns-

2016-10-24 and 2016-10-28

1
  • coming to my rescue again!
    – markb
    Commented Nov 3, 2016 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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