I don't know if I've complicated things a bit, so I'll explain things from the top. I have a set up with a custom post type (news), which is exposed via the REST API. The main endpoint (/wp-json/wp/v2/news
) returns a list of news
items, which needs to be sorted by popularity. I tried a few post view count plugins, but none of them seem to work via the REST API (they only work when the theme renders the post).
My plan is to create a "view count" ACF field and hook into the REST API init for a post (or only for my news
type), and update the value for that ACF field. I should then be able to request a list of the items ordered by the view count.
I seem to have found solutions for some of the tasks involved, but I can't figure out the basic one: How can I hook into WP when a REST API request is made on a particular post type and how can I update the custom field at that point?
ACF isn't an integral part of this query - I'm fine with using a native custom field, but if my question about the hook is answered, the field type is irrelevant.