$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Get all posts which was posted on X Days WordPress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Get all posts which was posted on X Days WordPress

matteradmin9PV0评论

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
Add a comment  | 

2 Answers 2

Reset to default 2

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 );

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
Post a comment

comment list (0)

  1. No comments so far