这是我第一次使用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
}
]
}
}
]