admin管理员组文章数量:1122832
After reading this answer here, I can get the currently selected/checked terms for categories using this code:
wp.data.select("core/editor").getCurrentPostAttribute("categories")
However, this doesn't seem to work with custom taxonomies created with register_taxonomy(). Is there any other way to do this?
This merge seems like it might be relevant, but it's kind of hard for me to understand what's being implemented there.
After reading this answer here, I can get the currently selected/checked terms for categories using this code:
wp.data.select("core/editor").getCurrentPostAttribute("categories")
However, this doesn't seem to work with custom taxonomies created with register_taxonomy(). Is there any other way to do this?
This merge seems like it might be relevant, but it's kind of hard for me to understand what's being implemented there.
Share Improve this question asked Jan 9, 2019 at 9:38 Aidan McArthurAidan McArthur 134 bronze badges2 Answers
Reset to default 3When you register a custom taxonomy make sure to set show_in_rest
to true
. This way the taxonomy will show in the REST API which is what Gutenberg uses to get the data.
Then you can use the selector:
wp.data.select("core/editor").getCurrentPostAttribute("my_taxonomy");
It's surprising how little information is available on this topic. Here’s my solution for retrieving the selected terms for a custom taxonomy:
const taxonomySlug = 'your-taxonomy';
// Get the selected series IDs
const selectedSeries = useSelect(( select ) =>
select('core/editor').getEditedPostAttribute(taxonomySlug),
);
// Get all series terms
const seriesTerms = useSelect(( select ) =>
select('core').getEntityRecords('taxonomy', taxonomySlug, {
object_id: postId,
}),
);
// Filter selected terms based on the selected series IDs
const selectedTerms = seriesTerms
? seriesTerms.filter(( term ) => selectedSeries.includes(term.id))
: [];
本文标签: Getting the selected terms for custom taxonomies in the editor
版权声明:本文标题:Getting the selected terms for custom taxonomies in the editor 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283368a1926944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论