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 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?
Share Improve this question edited Nov 15, 2018 at 7:25 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 15, 2018 at 6:52 Mr ShabirMr Shabir 13 bronze badges 1- I just need one date post. Like Only post which was posted on 1 SEP or which was posted on 25 Oct. – Mr Shabir Commented Nov 15, 2018 at 7:45
2 Answers
Reset to default 2You 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 );
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