How to get page tree view with Wordpress REST API 💻

Simple Approach:

The REST API basically returns query results. It doesn’t do the extra processing to assemble results into a tree structure. That is left for your code to do. You can use the Walker class to assemble data into a tree structure. This can either be done by your calling app (if it uses PHP) or you could create a custom API route/endpoint that queries and assembles data as desired.












People generally have trouble getting custom field data into your custom API response? If your API handler uses get_posts() or equivalent, you only get WP_Post objects, no meta data. Going that route, you’d need to get the meta data separately and merge the returned data. The alternative would be to build a custom SQL query that gets everything you want all at once.We’d normally verify that each field we place in an API response has its “show_in_rest” property set when it was registered. (in global $wp_meta_keys) This is to prevent sensitive data from leaking out through the API. If you’re sure you don’t have any sensitive data you might choose to skip this step.


























In case of any issues with REST API in Wordpress: Get in touch

0 Comments