0

All I'm trying to do here is a simple custom loop using WP_Query to display posts if they are published in the last 24 hours; otherwise, a message appears telling the user to check back soon for fresh posts. I'm trying to use the date_query parameter in my arguments but I'm getting unexpected results...at first it will work, but if I check the page in a few minutes, its as if it reset itself... my loop is set up below:

$args = array(
    'post_type' => 'surf_reports',
    'posts_per_page' => '1',
    'category_name' => $cat (this is pulled dynamically in my template),
    'date_query' => array(
            'before' => strtotime('-24 hours')
    )
);

And then obviously below there would be a loop, I'm not going to share that bc its standard and I believe unrelated to the issue.

  • 2
    The date query is an array of arrays and it understands strtotime() strings. Please let us know if that's not the problem here. – birgire Jul 13 '15 at 16:39
5

As birgire said date query is an array of arrays May be this solves your problemo.

$args = array(
  'post_type' => 'surf_reports',
  'posts_per_page' => '1',
  'category_name' => $cat (this is pulled dynamically in my template),
  'date_query' => array(
     array(
           'after' => '24 hours ago'
           )
     )
  );
  • I believe this solved the issue, just didn't have the second array. Thanks for the help. – la1ch3 Jul 14 '15 at 21:07
  • My pleasure buddy – terminator Jul 14 '15 at 21:43
0

before (string/array) - Date to retrieve posts before. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values:

I would try the before parameter with a value of 1 day or 24 hours

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.