• Resolved Jose Lazo

    (@jose-lazo)


    Hi!

    I need to get posts between dates in GMT via API but, according to the API doc, there is no way to do that. Only have modified_after and modified_before params.

    I had try to add &dates_are_gmt=1 flag (I saw in SOF), but nothing.

    Any one know how to get post by GMT dates?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There are also after and before params for the published dates. Usually params are logically ANDed together. Have you tried providing both params in one request?

    I think the expected times are in local time, but I’m not 100% sure. If I’m right, you’ll need to convert GMT to local for yourself with get_date_from_gmt().

    If providing both params does not provide the expected results, you could patch up the request’s query vars through the “pre_get_posts” action hook. A properly constructed “date_query” arg will surely result in posts within a specific range.

    Oddly the WP_Query doc still does not clearly indicate if times are GMT or local. I guess the focus is on dates and times don’t matter as much? It is possible to alter which date columns a query uses through the “posts_request” filter, but IMO it’d be better to convert to the right timezone before making the request.

    Thread Starter Jose Lazo

    (@jose-lazo)

    Thanks for you reply.

    My mistake ??. I mean to get posts between modified dates in GMT. I tried this:

    https://madridsecreto.co/wp-json/wp/v2/posts?per_page=100&modified_after=2022-09-01T00:00:00&modified_before=2023-10-13T13:00:00&dates_are_gmt=1 with no luck.

    You can see both dates in the response: modified and modified_gmt one.

    Maybe you are right and I need to patch up the request’s query vars through the “pre_get_posts” or register a custom endpoint ad hoc.

    Thanks for your time ??!

    • This reply was modified 10 months, 2 weeks ago by Jose Lazo.
    • This reply was modified 10 months, 2 weeks ago by Jose Lazo.
    Moderator bcworkz

    (@bcworkz)

    Yes, both modified_gmt and modified dates appear in the results. Specifying dates_are_gmt for the query does not change which fields are returned, all fields are returned. I think there’s a “fields” arg we can use, but its scope is limited. It cannot manage which date fields are returned.

    Why would it matter if both fields are returned? Use the one you want and ignore the other ??

    Passing both modified_after and modified_before seems to be working. I tried requesting &modified_after=2023-09-28T00:00:00&modified_before=2023-10-12T12:00:00 to limit the range so all matching posts can appear on one page. The posts returned all are within that range. AFAICT it works!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘API filter post by GMT dates’ is closed to new replies.