3

I want to know how to get previous 10 days post from a specific date, as I know that it can be done from current date as below

$args = array(
    'post_type' => 'post',
    'date_query'    => array(
        'after'   => '- 10 days'
    )
);
$query = new WP_Query( $args );

But I want posts previous 10 days post from a specific date. Is it possible ?

1 Answer 1

3

Yes, it is possible. You can pass full date as before and after params, so:

$given_date_as_time = strtotime('2017-11-22 00:00:00');
$args = array(
    'post_type' => 'post',
    'date_query' => array(
        'before' => date( 'c', $given_date_as_time ),
        'after' => date('c', strtotime( '- 10 days', $given_date_as_time ) ),
    ),
);
$query = new WP_Query( $args );

above query is also correct and will give you result you wanted.

2
  • Great, that worked for me. I am getting time in milliseconds, so directly using milliseconds is not working for me. Like ** 'before' => date( 'c', '1532995200000' )**
    – topper1309
    Jul 31, 2018 at 11:52
  • You should pass miliseconds as integer and not as string... And it should be timestamp (as result of time() function) and not miliseconds, I guess... Jul 31, 2018 at 12:00

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.