如何使用“getEntityRecords”获取特定分类术语

Mar*_*łoz 6 wordpress-rest-api wordpress-gutenberg gutenberg-blocks

我正在尝试使用“getEntityRecords”从特定的分类术语中获取自定义帖子类型。对于“post”,我可以在“query”对象中使用“categories”属性,如下所示:

getEntityRecords( 'postType', 'post', {per_page: 3, categories: [1,2,3] } )
Run Code Online (Sandbox Code Playgroud)

而且效果很好。但如何对分类术语做同样的事情呢?

我尝试在查询对象中使用“terms”属性,但这不起作用。

getEntityRecords( 'postType', 'my_post_type', {per_page: 3, terms: [1,2,3] } )
Run Code Online (Sandbox Code Playgroud)

我只想获取特定术语的帖子,但现在我获取所有自定义帖子。

小智 6

是的,没有很多关于使用数据模块的文档,并且使用它通常感觉像是猜测。然而,通过实验,我已经能够得出以下结论:

获取所有帖子:

getEntityRecords( 'postType', 'post' )
Run Code Online (Sandbox Code Playgroud)

要获取自定义帖子类型:

getEntityRecords( 'postType', 'your_custom_post_type')
Run Code Online (Sandbox Code Playgroud)

获取特定类别的自定义帖子

getEntityRecords( 'postType', 'your_custom_post_type', category: [1,2,3])
Run Code Online (Sandbox Code Playgroud)

要获取具有自定义分类条款的自定义帖子(我认为这是您问题的具体答案):

getEntityRecords( 'postType', 'your_custom_post_type', your_taxonomy: [1,2,3])
Run Code Online (Sandbox Code Playgroud)

要获取自定义分类法的术语:

getEntityRecords( 'taxonomy', 'your_taxonomy')
Run Code Online (Sandbox Code Playgroud)