get selected categories or tags (using javascript) in GutenBerg?

To get the categories from inside the editor of a post you can make use of the following selectors:

The categories the post has in the published version:
wp.data.select("core/editor").getCurrentPostAttribute("categories")

The current categories of the edit (for example if the user has selected a new category but hasn’t saved the post it will appear with this selector but not with the former):
wp.data.select("core/editor").getEditedPostAttribute("categories")

This will give you an array with the id of each category.

Leave a Comment