1

I would like to query WordPress draft posts only from the past 7 days (ie current date and looking back at the last 7 days, not looking at strictly Monday to Sunday weeks).

The problem I am facing at hand is that it does not return me any results at all. If I were to comment out the line that specifies the post_status, it works on published posts only.

EDIT: Problem is resolved. The issue at-hand causing no results to show up was pertaining to other parts of my code.

EDIT2: The code below can be significantly shortened to:

'date_query' => array(
    array(
        'after' => '1 week ago'
    )

My original code is as follows:

$today = getdate();

$args = array(
    'post_type' => array('post', 'page'),
    'post_status' => 'draft',
    'posts_per_page' => '-1',
    'date_query' => array(
        array(
            'after' => array(
                'year' => $today['year'],
                'month' => $today['mon'],
                'day' => ($today['mday'] - 7)
            ),
            'before' => array(
                'year' => $today['year'],
                'month' => $today['mon'],
                'day' => $today['mday']
            ),
            'inclusive' => true
        )
    )
);
  • that query seems to work fine for me, it returns draft posts within the past week. var_dump the query object after you run the query to see what it looks like. – Milo Jun 24 '18 at 4:15
  • Issue solved. The problem was pertaining to another part of my code. Thanks to @Milo for introducing me to var_dump which I didn't know prior to this! – infinityr00t Jun 24 '18 at 9:28
  • Your query will fail for days like the 3rd of the month. You can't just - 7 on that, better to use DateTime::modify() – kero Jun 24 '18 at 20:04
  • 1
    Thanks @kero for the pro-tip! That issue slipped my mind. – infinityr00t Jun 27 '18 at 1:38

Your Answer

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

Browse other questions tagged or ask your own question.