Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 使用withSelect在Gutenberg block中选择具有特定术语的帖子_Wordpress_Wordpress Gutenberg - Fatal编程技术网

Wordpress 使用withSelect在Gutenberg block中选择具有特定术语的帖子

Wordpress 使用withSelect在Gutenberg block中选择具有特定术语的帖子,wordpress,wordpress-gutenberg,Wordpress,Wordpress Gutenberg,我试图选择属于特定术语的帖子——或者更具体地说,是一种自定义帖子类型——并将它们显示在我的Gutenberg block的编辑屏幕中。我想用WP_查询重新创建tax_查询,但使用Javascript 我可以选择文章,但我不确定getEntityRecords使用哪些参数来按术语选择(或者是否可能)。文档在这一点上仍然有点模糊 这就是我要说的。这将成功选择“rmenu”类型的所有帖子: const items = select("core").getEntityRecords( "post

我试图选择属于特定术语的帖子——或者更具体地说,是一种自定义帖子类型——并将它们显示在我的Gutenberg block的编辑屏幕中。我想用WP_查询重新创建tax_查询,但使用Javascript

我可以选择文章,但我不确定getEntityRecords使用哪些参数来按术语选择(或者是否可能)。文档在这一点上仍然有点模糊

这就是我要说的。这将成功选择“rmenu”类型的所有帖子:

const items = select("core").getEntityRecords(
    "postType",
    "rmenu"
);
有人知道getEntityRecords是否是处理此问题的正确方法吗

谢谢。

的第三个参数是
查询
对象。您可以传递诸如
类别
标记
。按类别选择术语如下所示:

const items = select("core").getEntityRecords(
  "postType",
  "rmenu",
  { categories: [ 13 ] }
}
{
    title: "hello world",
    content: "this is a post",
    id: 123,
    type: 'my-custom-posttype'
    my-custom-tax: [5, 8, 24],
    ...
}

虽然Capi的答案是正确的,但我不清楚这是否也适用于自定义分类法。事实证明wp.data会自动将自定义分类法作为属性添加到post对象中。例如,一篇文章可以如下所示:

const items = select("core").getEntityRecords(
  "postType",
  "rmenu",
  { categories: [ 13 ] }
}
{
    title: "hello world",
    content: "this is a post",
    id: 123,
    type: 'my-custom-posttype'
    my-custom-tax: [5, 8, 24],
    ...
}
因此,为了获取分类法
我的定制税
中ID为
4
8
my custom posttype
类型的所有帖子,您可以运行以下查询:

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

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

我注意到的一件事是对象“items”确实包含每个post的术语。因此,一种解决方法是查询所有帖子,然后遍历它们并比较术语ID。似乎有更好的方法可以直接选择帖子,除非WP 5.0的早期版本中没有。