Custom field data and custom Rest API fields
-
Hi all,
I’ve been trying to expand my WordPress installation beyond just blogs and pages by creating a custom post type. The end-goal is to have a separate Rest API endpoint with custom fields depending on my needs.
It’s going fairly well and I’m almost done, however I’m unclear on how meta attributes are supposed to work with the Rest API.
Here’s what I’ve done so far:
- Created a custom post type with a Rest API endpoint
- Created a custom block that saves data to my ‘meta’ field
My custom data is now shown inside the ‘meta’ object in my Rest API like this:
meta: { paletteData: [ "#f6f9f4", "#2294c5", "#323333", "#131415", "#a3001d" ] }
However I’d rather not put all my custom data inside
meta
as that might look a bit messy. Which is why I then usedregister_rest_field
to display the above data. outside ofmeta
.Unfortunately now my data is shown in two different objects, and if I try to hide my
meta
object, neither my custom Rest field nor my editor block will work.So here’s my questions:
- should I be saving my custom data inside
meta
or should I do something else to save it? - Is there any way I can show my custom data inside
palette: {}
rather thanmeta: {palette: {}}
?
You can see an example of my live Rest API here if that helps.
I’ve also uploaded a plugin of my code to github. The plugin adds:
- A custom post type
- A rest api endpoint at
/wp-json/wp/v2/featuredsites/
- A block called
Palette Meta Block
which allows to pick a colors which are saved withinmeta: { palette: {}}
. - a rest field which prints the
palette
custom field data insidepalette:{}
You should be able to use it to test how my code works.
(PS the block doesn’t work great, I’m still working on it.)Sorry in advance for the messy code, the plan was to clean it up once it was fully done.
- The topic ‘Custom field data and custom Rest API fields’ is closed to new replies.