如何通过接口按子值查询帖子?

时间:2017-05-09 作者:JacobTheDev

这是我第一次使用REST,我很难通过元键获得帖子。我已经添加了需要使用的元键register_meta, 并在上运行请求/wp-json/wp/v2/products, 输出中确实包含我的自定义字段。

我知道我可以添加?slug={some slug}, 或?id={some id} 通过这些值检索帖子,在测试中效果很好。但我不知道如何解析自定义字段,因为它们嵌套在“meta”列表下的JSON数据中。

在谷歌上搜索了大约一个小时后,我开始猜测我应该做什么。我试过很多事情,比如?brand_name=test, ?meta[brand_name]=test, 和?[meta]brand_name=test, 但这些似乎都没有起到任何作用。

我肯定我错过了一些明显的东西;我非常感谢你的帮助。下面是我的JSON数据示例。

[
   {  
      "id":5,
      "date":"2017-05-04T10:36:10",
      "date_gmt":"2017-05-04T15:36:10",
      "guid":{  
         "rendered":"http:\\/\\/products.example.localhost\\/?post_type=product&p=5"
      },
      "modified":"2017-05-09T13:41:12",
      "modified_gmt":"2017-05-09T18:41:12",
      "slug":"12545xa573",
      "status":"publish",
      "type":"product",
      "link":"http:\\/\\/products.example.localhost\\/products\\/12545xa573\\/",
      "title":{  
         "rendered":"12545XA573"
      },
      "excerpt":{  
         "rendered":"",
         "protected":false
      },
      "author":1,
      "featured_media":32,
      "comment_status":"open",
      "ping_status":"open",
      "template":"",
      "format":"standard",
      "meta":{  
         "brand_name":"test",
         "sku":"12545XA573",
         "unit_of_measure":"FT, 1\\/8, 1\\/16",
         "length":"3.20 in"
      },
      "_links":{  
         "self":[  
            {  
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/products\\/5"
            }
         ],
         "collection":[  
            {  
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/products"
            }
         ],
         "about":[  
            {  
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/types\\/product"
            }
         ],
         "author":[  
            {  
               "embeddable":true,
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/users\\/1"
            }
         ],
         "replies":[  
            {  
               "embeddable":true,
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/comments?post=5"
            }
         ],
         "version-history":[  
            {  
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/products\\/5\\/revisions"
            }
         ],
         "wp:featuredmedia":[  
            {  
               "embeddable":true,
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/media\\/32"
            }
         ],
         "wp:attachment":[  
            {  
               "href":"http:\\/\\/products.example.localhost\\/wp-json\\/wp\\/v2\\/media?parent=5"
            }
         ],
         "curies":[  
            {  
               "name":"wp",
               "href":"https:\\/\\/api.w.org\\/{rel}",
               "templated":true
            }
         ]
      }
   }
]

1 个回复
最合适的回答,由SO网友:hwl 整理而成

要通过元键访问帖子,您需要连接到[rest_query_vary 过滤器]。1 当我读到你的问题时,我想到的一个例子是the same one found on the post @WebElaine 已发表评论。我只是在下面加了一句:

function my_allow_meta_query( $valid_vars ) {

    $valid_vars = array_merge( $valid_vars, array( \'meta_key\', \'meta_value\' ) );
    return $valid_vars;
}
add_filter( \'rest_query_vars\', \'my_allow_meta_query\' );
像这样打电话:wp-json/wp/v2/posts?filter[meta_key]=MY-KEY&filter[meta_value]=MY-VALUE

过滤器作为WP_REST_Posts_Controller::prepare_items_query, 如有帮助,请在此上下文中显示:

protected function prepare_items_query( $prepared_args = array(), $request = null ) {
    $query_args = array();

    foreach ( $prepared_args as $key => $value ) {
        /**
         * Filters the query_vars used in get_items() for the constructed query.
         *
         * The dynamic portion of the hook name, `$key`, refers to the query_var key.
         *
         * @since 4.7.0
         *
         * @param string $value The query_var value.
         */
        $query_args[ $key ] = apply_filters( "rest_query_var-{$key}", $value );
    }

    if ( \'post\' !== $this->post_type || ! isset( $query_args[\'ignore_sticky_posts\'] ) ) {
        $query_args[\'ignore_sticky_posts\'] = true;
    }

    // Map to proper WP_Query orderby param.
    if ( isset( $query_args[\'orderby\'] ) && isset( $request[\'orderby\'] ) ) {
        $orderby_mappings = array(
            \'id\'      => \'ID\',
            \'include\' => \'post__in\',
            \'slug\'    => \'post_name\',
        );

        if ( isset( $orderby_mappings[ $request[\'orderby\'] ] ) ) {
            $query_args[\'orderby\'] = $orderby_mappings[ $request[\'orderby\'] ];
        }
    }

    return $query_args;
}

结束

相关推荐

WP_AJAX未调用该操作

我正在开发一个Ajax不起作用的插件。我正在使用以下代码:add_action( \'wp_ajax_cpm_add_update\', array( $this, \'edit_added_people\' ) ); Theedit_added_people() 调用操作后未调用函数cpm_add_update, 这可能是什么原因?以下是我的表单代码:function cpm_add_people_form( $project_id, $people = null ) {