wordpress-rest-api - 如何将 "getEntityRecords"用于特定的分类术语

标签 wordpress-rest-api wordpress-gutenberg gutenberg-blocks

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

getEntityRecords( 'postType', 'post', {per_page: 3, categories: [1,2,3] } )
它工作正常。但是如何对分类术语做同样的事情呢?
我试过在查询对象中使用“terms”属性,但这不起作用。
getEntityRecords( 'postType', 'my_post_type', {per_page: 3, terms: [1,2,3] } )
我只想获得特定期限的帖子,但现在我获得了所有自定义帖子。

最佳答案

是的,没有很多关于使用数据模块的文档,而且使用它通常感觉像是在猜测。但是,通过实验,我已经能够解决以下问题:
获取所有帖子:

getEntityRecords( 'postType', 'post' )
要获取自定义帖子类型:
getEntityRecords( 'postType', 'your_custom_post_type')
获取具有特定类别的自定义帖子
getEntityRecords( 'postType', 'your_custom_post_type', category: [1,2,3])
要获得具有自定义分类条款的自定义帖子(我认为这是您问题的具体答案):
getEntityRecords( 'postType', 'your_custom_post_type', your_taxonomy: [1,2,3])
要获取自定义分类的术语:
getEntityRecords( 'taxonomy', 'your_taxonomy')

关于wordpress-rest-api - 如何将 "getEntityRecords"用于特定的分类术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57878714/

相关文章:

wordpress - 在wordpress中加载 block 时运行一段代码

wordpress-gutenberg - Gutenberg RichText 光标聚焦自动聚焦出

json - 如何呈现wordpress api的内容?

php - 为什么我不能从 WP_REST_Request 访问查询参数?

wordpress - 古腾堡 WordPress : Extending Core Block

wordpress - 古腾堡自定义 block php 渲染问题

wordpress-rest-api - 通过过滤器或钩子(Hook)从 wp rest api 响应中删除 _links 对象

javascript - 如何通过 wordpress rest api 从自定义表中获取所有记录?

javascript - WordPress:将对齐中心添加到古腾堡组 block

wordpress - 防止 wp.hooks.addFilter() 在 Gutenberg 的某些自定义帖子类型上运行