0

I want to get all post which was posted on X Days, I don't want 7 days old post or 1 Month old posts, Because it will return all posts which will be older 7 days or one month. Like Today is 15 SEP, I want to get all posts which was post on 5 SEP. Only 5 sep not older then 5 SEP. Only one Day posts. I have try code like this

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
    )
);

But its return 10 days old posts. Any help to get post for X days?

  • I just need one date post. Like Only post which was posted on 1 SEP or which was posted on 25 Oct. – Mr Shabir Nov 15 '18 at 7:45
1

You can try following code

$y = date('Y');
$m = date('m');
$d  = date('d',strtotime("-7 days")); //Date you want

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'date_query' => array(
        array(
            'year'  => $y,  
            'month' => $m,
            'day'   => $d,
        ),
    ),
);
$posts= new WP_Query( $args ); //you will get posts which are posted on specific date
  • This will return all posts which is older then 7 days, Even it will return posts from 2015. – Mr Shabir Nov 15 '18 at 7:23
  • No, it will return posts from given day. There is another bug in that code - all dates have to be changed by 7 days. Otherwise it won’t work at first 7 days of month. – Krzysiek Dróżdż Nov 15 '18 at 7:24
  • You can adjust date parameters – Aparna_29 Nov 15 '18 at 7:24
1

You can use 'date_query' to achieve exactly what you need. All you have to do is not to use 'before' or 'after', but only date (the way WordPress does when you go to day archive).

Here’s the code you can use:

$date = strtotime('-10 days');

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'date_query' => array(
        array(
            'year'  => date('Y', $date),  
            'month' => date('m', $date),
            'day'   => date('d', $date),
        ),
    ),
);
$posts= new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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