I am trying to get the current tag list for a given post inside the save function to render on page.
There is a similar Q here: How to return a list of custom taxonomy terms via the Gutenberg getEntityRecords method, but I am not sure if that is the solution to my problem?
This taxonomy is set to 'show_in_rest' => TRUE,
.
A couple of attempts I have tried are:
save: function() {
// #1:
var tag_array = wp.data.select("core/editor").getCurrentPostAttribute("custom_taxonomy");
var tag_list = [];
if (tag_array) {
tag_array.forEach(
tag => tag_list.push(
el('li', null, wp.data.select('core').getEntityRecord('taxonomy', 'custom_taxonomy', tag))
),
);
}
// #2:
const tag_array = wp.data.select("core/editor").getCurrentPostAttribute("custom_taxonomy");
const tag_objects = wp.data.select('core').getEntityRecords('taxonomy', 'custom_taxonomy', tag_array);
var tag_list = [];
tag_objects.forEach(
tag => tag_list.push(
el('li', null, tag.name)
)
);
}
Attempt #1 returns objects
Attempt #2 does not filter the results to the given current tag ID's and returns the entire list of tags in the taxonomy.
wp.data.select("core/editor").getCurrentPostAttribute("custom_taxonomy");
returns the correct tag ID's.
Is there a method to load a tag name with the ID (other than .getEntityRecord()
)?
A bit unsure what I'm missing here, how can I get a list of selected tags for the current post?