Add Custom Field to WordPress API & Filter

I found myself in a strange situation where I needed to know if a page had a child. Natively the WP API lets you know if a page has a parent but not the reverse. First I had to write a function that writes some data to a custom field if a page has a child. Then I started writing this post and realized I needed to do the reverse and now I’ve written a function that writes data to a parent when the child is created. Now that I had the data being written to the custom field, I needed to make that data visible in the WP REST API. Luckily, Jeff put up a snippet for that not too long ago. Now I also wanted to be able to return data based on the contents of the has_children field. This filter1 does that. Now a URL like the one below will give me pages with children but without parents. /wp-json/wp/v2/pages?_embed&per_page=30&has_children=1&parent=0 This will help me make a fairly large menu structure more manageable and I figured having something like this all in one place might help someone else. 1 I usually call things ‘chunks’ of code but I’m trying to improve my vocab.