[使用Gutenberg块中的withSelect选择具有特定术语的帖子

问题描述 投票:2回答:2

我正在尝试选择属于特定术语的帖子,或更具体地说,是自定义帖子类型,并将其显示在我的Gutenberg块的编辑屏幕中。我想用WP_Query重新创建tax_query,但是使用Javascript。

我可以选择帖子,但是我不确定与getEntityRecords一起使用哪些参数来按术语选择(或者甚至可以选择)。此时文档仍然有点模糊。

我在这里。这将成功选择“ rmenu”类型的所有帖子:

const items = select("core").getEntityRecords(
    "postType",
    "rmenu"
);

有人知道getEntityRecords是否是处理此问题的正确方法吗?

谢谢。

wordpress wordpress-gutenberg
2个回答
0
投票

getEntityRecords的第三个参数是query对象。您可以传递any query argument accepted by Wordpress API,例如categoriestags。按类别字词选择如下所示:

const items = select("core").getEntityRecords(
  "postType",
  "rmenu",
  { categories: [ 13 ] }
}

0
投票

尽管Capi的回答是正确的,但对我来说,这还不适用于自定义分类法。事实证明,wp.data会将您的自定义分类法自动添加为post对象的属性。例如,帖子可能看起来像这样:

{
    title: "hello world",
    content: "this is a post",
    id: 123,
    type: 'my-custom-posttype'
    my-custom-tax: [5, 8, 24],
    ...
}

因此,为了获得类型为my-custom-posttype且其术语为分类法4的ID为8my-custom-tax的所有帖子,您可以运行以下查询:

wp.data.select( 'core' ).getEntityRecords( 'postType', 'my-custom-posttype', { 'my-custom-tax':[4,8] })

重要!如果在浏览器中对此进行测试,则需要运行两次。第一次它将返回一个空数组,因为它仅调用promise。

© www.soinside.com 2019 - 2024. All rights reserved.