0

I am trying to perform a WP_Query, passing the values for before and after in the date_query as unix timestamps.

This does not seem to work:

$args = array(
  'date_query' => array(
    'before' => 1486045020
  )
);

I know I could use php's date() function to format it to a year, month and day, but I would rather use a unix timestamp to avoid time difference issues.

0

You can have a look at the blog database itself to see the exact format Wordpress stores dates in. It doesn't use Unix timestamps. I believe the format is something like 2017-01-31 (YYYY-MM-DD). I think you might have to use the format Wordpress uses, then convert to/from Unix timestamps when you need to.

EDIT, either that or you might be able to use something like the_date where you can specify the format.

Formatting Date and Time

0

I found the solution - I used PHP's date() function to format it into a ISO 8601 compliant format, so there is no confusion around timezones, etc.

$args = array(
  'date_query' => array(
    'before' => date( 'c' , 1486045020 )
  )
);

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.