I'm pretty new to the custom API endpoints for wordpress. I'm currently trying to display a certain taxonomy (which I have done so using the basic show_in_rest
thus creating an endpoint as /wp-json/wp/v2/platforms
). This works great, however there's a lot of unnecessary information provided at that endpoint (such as meta, yoast header, links, description, etc) and it also makes it kind of slow considering there's 247 of them. We created this taxonomy using ACF, if that matters.
My goal is to provide an easy way to dynamically populate a gravity form field with all of the PARENT platforms, and when the user selects a parent platform, the secondary field shows all CHILD platforms of that parent. How can that be done? We are currently using .ajax()
to get those queries, but it's super slow. The ultimate goal was to create a custom API endpoint so we can just pull the list of platforms and have them populate the fields appropriately.
If anyone knows of an easier way, that would be welcomed too ^.^
So you wrote you want to display only the parent terms (platforms) in the first dropdown. That means you want only those terms that have no parent (i.e. parent 0
).
The term collection API endpoint supports the parent
argument (see here and here). You should provide that argument when fetching (i.e. /wp-json/wp/v2/platforms?parent=0
). Then you should do the same with the non-zero parent too for the second level dropdown. This should limit the number of results so that the performance is good enough. If response time is still bad for 10-20 results, you should check for the problem elsewhere (e.g. 3rd-party plugins).
You could of course create a new endpoint which would return only the fields you need, but it seems too much work for 247 entries for which there is the default endpoint...