1

My initial situation is, that I have a custom post type events which are - who would have thought - events. There is a custom UI (backend) where the user can enter multiple dates. Each date is then saved as post meta as a timestamp.

I am then using pre_get_posts (and a few other filters/actions) to change the query so the posts are displayed based on that timestamp. This is working perfectly when calling the default post type archive page (http://www.example.com/[post-type-slug]/).

We are currently developing an app for mobile phone which then should display the events. We are using the WP REST API to retrive the event data. The problem is that is_main_query() returns always false when using the REST API.

Does anybody know how i can bypass this problem?

1 Answer 1

2

To answer the question directly - The REST API do not initialize a main query, therefor there should not be any for requests coming that way.

What you should do is to create your own end point and server what ever custom data you need on it. Modifying the REST API, while possible, violates the whole idea of having a consistent and documented API in the first place.

1
  • Thanks four your reply, after playing around with the API a bit this makes sense now. First I thought requesting posts via REST API is "parallel" to normal requests and WordPress uses the same mechanisms to handle REST requests as usual requests, but that is NOT true. The REST API is using its own mechanisms to parse and handle requests, totally unrelated to normal requests.
    – Stefan
    Feb 5, 2018 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.