【问题标题】:How to use "getEntityRecords" for specific taxonomy terms如何对特定分类术语使用“getEntityRecords”
【发布时间】:2020-01-12 16:17:29
【问题描述】:

我正在尝试使用“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] } )

我只想获得特定期限的帖子,但现在我获得了所有自定义帖子。

【问题讨论】:

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


    【解决方案1】:

    好的,我找到了解决方案。我所做的只是注册分类参数。

    add_filter('register_taxonomy_args', 'my_function', 10, 2);
    function my_function ($args, $taxonomy)
    {
        if ( in_array( $taxonomy, array('taxonomy1', 'taxonomy2' ) ) )
        {
            $args['show_in_rest'] = true;
        }
    
        return $args;       
    }
    

    然后我用过:

    getEntityRecords( 'postType', 'post', {per_page: 3, taxonomy1: [1,2,3] } )
    

    【讨论】:

      【解决方案2】:

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

      获取所有帖子:

      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')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-20
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        相关资源
        最近更新 更多