我试图通过wpapi创建一个post,并使用python发送http请求。因此,http请求主体的示例如下所示:
body = json.dumps(dict(
slug='test',
status='publish',
title='test',
excerpt='test',
content='test',
author=1,
comment_status='open',
ping_status='open',
categories=[1],
meta={
'_links_to': 'https://google.com',
'_knawatfibu_url': 'https://some-image.jpg'
}
))
发送本身是这样的:
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + base64.b64encode(f'{settings.WP_LOGIN}:{settings.WP_PASS}'.encode()).decode()
}
response = requests.post(settings.WP_HOST + '/wp-json/wp/v2/posts', json=data, headers=headers)
好消息是它创建了post。你知道吗
但是它没有设置元字段,这使得一些插件没有效果。如何通过API执行元字段的设置?你知道吗
我听说我需要通过PHP代码公开某些元字段。但我该怎么做,最重要的是-我该在哪里做?你知道吗
编辑:
我尝试将这段PHP代码添加到函数.php并认为它显示了API GET调用中的所有元字段,但设置这些字段仍然是不可能的。你知道吗
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'meta', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id );
}
找到了答案。你知道吗
只需将此PHP代码片段放入函数.php:
相关问题 更多 >
编程相关推荐