这是 Postman 为成功调用我的页面而提供的(修改后的)片段。
var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Basic anVyYTp3MmZacmo2eGtBOHJsRWrt");
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
但是当放置在我的 c# 应用程序中时,它返回 403 forbidden,而 Postman 生成它并收到 200。当我在我的应用程序中使用 httpclient 时会发生同样的事情(403)。
我正在按照本教程创建 WP-API 的自定义端点。
在邮递员上点击/wp-json/custom-plugin/v2/get-all-post-ids/进行测试时,我总是收到此错误:
{
"code": "rest_no_route",
"message": "No route was found matching
the URL and request method ",
"data": {
"status": 404
}
}
Run Code Online (Sandbox Code Playgroud)
我在 /plugins/custom-plugin/ 目录中创建了一个 custom-plugin.php 文件。
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
add_action( 'rest_api_init', 'dt_register_api_hooks' );
function dt_register_api_hooks() {
register_rest_route( 'custom-plugin/v2', '/get-all-post-ids/', array(
'methods' => 'GET',
'callback' => 'dt_get_all_post_ids',
)
);
}
// Return all post IDs
function dt_get_all_post_ids() {
if ( false === ( $all_post_ids = …Run Code Online (Sandbox Code Playgroud) 我有一个自定义帖子类型“产品”,我使用高级自定义字段(ACF) 插件为其创建了自定义字段。
\n\n当我请求每个产品都有acf 属性的产品时,例如:/wp-json/wp/v2/products
"acf": {\n "rating": 77\n "fun-factor": 10,\n "something": 65,\n \xe2\x80\xa6\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在,我products按我的属性进行过滤/wp-json/wp/v2/products?filter[meta_key]=rating&filter[meta_value]=77,并将结果返回到上面的产品。到目前为止,一切都很好。
现在我有两件事想要完成:
\n\n我很高兴收到任何意见!
\n是否有任何解决方案可以在以下端点的响应中获取类别名称而不是 id? /wp-json/wp/v2/posts/:post_id
谢谢你们
我试图抓住帖子并按日期按降序排序.
我在谈论这个API:https://github.com/WP-API/WP-API
我试过了:
http://url.com/json/wp-json/wp/v2/posts?filter[order]=DESC
http://url.com/json/wp-json/wp/v2/posts?order=desc
它们都不起作用.
但是,例如,这可以正常工作:
http://url.com/json/wp-json/wp/v2/posts?filter[cat]=3
任何的想法?
我正在使用内置的WP REST API来创建帖子(自定义帖子类型).我已经完成了这部分工作,下面是我提交的一些JSON示例,用于创建新帖子.
{
"title": "The story of Dr Foo",
"content": "This is the story content",
"status":"publish",
"excerpt":"Dr Foo story"
}
Run Code Online (Sandbox Code Playgroud)
问题是,我如何传递分配的分类?我似乎无法找到任何人询问或如何做的痕迹?
对于上下文,server 1正在创建帖子和WP存在server 2.
我还尝试使用请求传递长元数据,然后我可以在WP服务器上循环并使用wp_set_post_terms()相应地设置分类.
为此,有一个(几乎)rest_insert _ {$ this-> post_type}在通过REST API创建或更新单个帖子后触发.
此方法的问题在于,在action_rest_insert_post_type()函数完成之后才会设置元数据.这意味着当我尝试检索当前的帖子元数据时,它不存在.
function action_rest_insert_post_type( $post, $request, $true ) {
$items = get_post_meta( $post->ID); // at this point in time, returns empty array.
//wp_set_terms()
}
Run Code Online (Sandbox Code Playgroud)
我已经确认它$post->ID正在按预期工作,只是元数据没有完全设置...
请指教.
我试图通过WordPress REST API 2.0-beta15和WordPress 获取(过滤)特定日期后修改的帖子v4.8.3,并使用我的客户端应用程序中的现有帖子进行更新.
使用after和before查询WordPress提供的参数我可以基于date而不是modified字段来过滤帖子.
我已尝试/wp-json/wp/v2/posts?filter[date_query][0][column]=post_modified_gmt&filter[date_query][0][after]=1+month+ago使用此问题中提到的https://github.com/WP-API/rest-filter,但此过滤器现在也无法正常工作.date_query
我需要任何选择
http://bankersdaily.in/wp-json/wp/v2/posts?modified_after=2017-10-31T13:32:10&_envelope http://bankersdaily.in/wp-json/wp/v2/posts?after=2017 -10-31T13:32:10&字段=修饰_envelope
参考文献:
https://developer.wordpress.org/rest-api/reference/posts/#list-posts https://codex.wordpress.org/Class_Reference/WP_Query?#Date_Parameters
我想通过过滤添加了高级自定义字段的自定义元来查询我的帖子。这是一个布尔元,所以每个帖子都会有类似的内容:
{
...
"acf" : {
"highlight" : true
}
...
}
Run Code Online (Sandbox Code Playgroud)
即使我将 meta_key 和 meta_value 暴露给 REST API,我也无法按此元值进行过滤function.php:
function my_add_meta_vars ($current_vars) {
$current_vars = array_merge ($current_vars, array ('meta_key', 'meta_value'));
return $current_vars;
}
add_filter ('rest_query_vars', 'my_add_meta_vars');
Run Code Online (Sandbox Code Playgroud)
但如果我尝试:
获取 http://localhost/wp-json/wp/v2/posts?filter[meta_key]=highlight&filter[meta_value]=true
我看到所有的帖子就好像过滤器被忽略一样。
我认为我已经完成了90%的工作,但是最终“上传”了空白的透明图像。上传后,我收到201回应。我认为这可能是WP发现丢失图像的代理。我不确定是否传递了错误的图像(即它没有离开我的计算机),或者我是否没有根据WP的喜好正确标记图像。
from base64 import b64encode
import json
import requests
def imgUploadREST(imgPath):
url = 'https://www.XXXXXXXXXX.com/wp-json/wp/v2/media'
auth = b64encode('{}:{}'.format('USERNAME','PASS'))
payload = {
'type': 'image/jpeg', # mimetype
'title': 'title',
"Content":"content",
"excerpt":"Excerpt",
}
headers = {
'post_content':'post_content',
'Content':'content',
'Content-Disposition' : 'attachment; filename=image_20170510.jpg',
'Authorization': 'Basic {}'.format(auth),
}
with open(imgPath, "rb") as image_file:
files = {'field_name': image_file}
r = requests.post(url, files=files, headers=headers, data=payload)
print r
response = json.loads(r.content)
print response
return response
Run Code Online (Sandbox Code Playgroud)
我已经在php或node.js中看到了很多答案,但是我在理解python的语法时遇到了麻烦。感谢您的任何帮助!
我正在尝试使用WP API v2并插入Postman的帖子.
如果我发布这个原始请求,它会创建一个帖子就好了:
{
"title": "Test Title",
"content": "Test Content",
}
Run Code Online (Sandbox Code Playgroud)
但是,我正在尝试为此添加一些自定义字段值,我似乎无法让它们工作.此请求会创建一个帖子,但不会添加任何元字段:
{
"title": "Test Title",
"content": "Test Content",
"meta": {
"foo": "bar",
"foo2": "bar2"
}
}
Run Code Online (Sandbox Code Playgroud)
如何发布元字段foo以及foo2值bar和bar2API端点https://my-site.com/wp-json/wp/v2/posts?
编辑:似乎自定义字段不会在GET请求中本地拉出.我把这个代码放在一个mu-plugin中:
add_filter( 'rest_prepare_post', 'xhynk_api_post_meta', 10, 3 );
function xhynk_api_post_meta( $data, $post, $context ){
$meta = get_post_custom( $post->ID );
if( $meta ) {
$data->data['meta'] = $meta;
}
return $data;
}
Run Code Online (Sandbox Code Playgroud)
至少让我在GET请求中查看它.但是我仍然无法通过Postman将其发送到POST.即使添加"status": "publish"也会导致新帖子发布而不是默认情况下的草稿.我可以在API POST请求中使用任何钩子或过滤器来确保添加自定义字段吗?