gpt4 book ai didi

wordpress - 如何通过 WP REST API V2 使用自定义字段正确创建自定义帖子类型

转载 作者:行者123 更新时间:2023-12-02 23:17:10 28 4
gpt4 key购买 nike

如何通过带有自定义字段的 wp Rest api v2 正确创建自定义帖子类型记录?无法创建自定义字段或更新它们。

我已在自托管 WordPress 安装中安装了 superlist 和 superlist-child 主题。

此主题对类别、位置、称为企业的自定义帖子类型以及自定义帖子类型的帖子元中的自定义字段使用自定义分类法

我正在尝试使用 Rest api v2 将数据导入到站点,并已成功创建自定义类别和位置。我使用 REST API Enabler 插件来注册自定义端点和自定义字段。

使用业务端点http://mysite/wp-json/wp/v2/businesses我可以通过api上传新业务,但创建的记录会忽略所有元键

例如给定以下正文

      body = {
title: company_name,
listing_address: [address],
listing_map_location_latitude: [c.lat],
listing_map_location_longitude: [c.lon],
content: c.description,
email: c.email,
facebook: [c.facebook],
image_url: c.image_url,
locations: [c.city.server_id],
listing_categories: c.categories.pluck(:server_id),
listing_street_view: ["on"]
status:"publish"
}
request = HTTParty.post(POSTS_END_POINT, body: body, headers: headers)

将业务正确附加到之前通过 REST API 创建的正确位置和自定义类别 (listing_categories),但是没有填充任何自定义字段(wp_postmeta 表),例如listing_locations 经度和纬度字段以及 listing_address

我可以查询通过rest api上传的记录,我得到这样的响应

$ rake upload:test
Result: {"id"=>1749, "date"=>"2016-02-20T05:06:01", "date_gmt"=>nil, "guid"=>{"rendered"=>"http://highstreetbeacons.com/?post_type=business&p=1749"}, "modified"=>"2016-02-20T05:06:01", "modified_gmt"=>nil, "slug"=>"", "type"=>"business", "link"=>"http://highstreetbeacons.com/?post_type=business&p=1749", "title"=>{"rendered"=>"Birmingham Museum and Art Gallery, Chamberlain Square, Birmingham, B3 3DH"}, "content"=>{"rendered"=>"<p>A description</p>\n"}, "author"=>1, "featured_media"=>0, "comment_status"=>"open", "ping_status"=>"closed", "listing_categories"=>[3779, 4095, 4061], "locations"=>[330], "_links"=>{"self"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/businesses/1749"}], "collection"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/businesses"}], "about"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/types/business"}], "author"=>[{"embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/users/1"}], "replies"=>[{"embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/comments?post=1749"}], "https://api.w.org/attachment"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/media?parent=1749"}], "https://api.w.org/term"=>[{"taxonomy"=>"listing_categories", "embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/listing_categories?post=1749"}, {"taxonomy"=>"locations", "embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/locations?post=1749"}]}}

这完全忽略了元键。如果在管理区域中我手动点击生成业务的更新按钮,我可以通过 api 重新查询并获取返回的所有正确字段,但更新的 POST 请求(当然应该是 PUT)记录仍然不会设置任何自定义字段

例如

rake upload:test
Result: {"id"=>1745, "date"=>"2016-02-20T04:53:17", "date_gmt"=>nil, "guid"=>{"rendered"=>"http://highstreetbeacons.com/?post_type=business&#038;p=1745"}, "modified"=>"2016-02-20T04:53:17", "modified_gmt"=>"2016-02-20T04:53:17", "slug"=>"", "type"=>"business", "link"=>"http://highstreetbeacons.com/?post_type=business&p=1745", "title"=>{"rendered"=>"Birmingham Museum and Art Gallery, Chamberlain Square, Birmingham, B3 3DH"}, "content"=>{"rendered"=>"<p>A description</p>\n"}, "author"=>1, "featured_media"=>0, "comment_status"=>"open", "ping_status"=>"closed", "listing_categories"=>[3779, 4095, 4061], "locations"=>[330], "listing_featured_image"=>[""], "listing_banner"=>["banner_featured_image"], "listing_opening_hours"=>["a:7:{i:0;a:4:{s:11:\"listing_day\";s:6:\"MONDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:1;a:4:{s:11:\"listing_day\";s:7:\"TUESDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:2;a:4:{s:11:\"listing_day\";s:9:\"WEDNESDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:3;a:4:{s:11:\"listing_day\";s:8:\"THURSDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:4;a:4:{s:11:\"listing_day\";s:6:\"FRIDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:5;a:4:{s:11:\"listing_day\";s:8:\"SATURDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}i:6;a:4:{s:11:\"listing_day\";s:6:\"SUNDAY\";s:17:\"listing_time_from\";s:0:\"\";s:15:\"listing_time_to\";s:0:\"\";s:14:\"listing_custom\";s:0:\"\";}}"], "listing_map_location"=>["a:2:{s:8:\"latitude\";s:0:\"\";s:9:\"longitude\";s:0:\"\";}"], "listing_street_view_location_latitude"=>["37.812405"], "listing_street_view_location_longitude"=>["-122.47607800000003"], "listing_street_view_location_heading"=>["-18"], "listing_street_view_location"=>["a:5:{s:8:\"latitude\";s:9:\"37.812405\";s:9:\"longitude\";s:19:\"-122.47607800000003\";s:4:\"zoom\";s:1:\"1\";s:7:\"heading\";s:3:\"-18\";s:5:\"pitch\";s:2:\"25\";}"], "listing_locations"=>["a:1:{i:0;s:10:\"birmingham\";}"], "listing_listing_category"=>["a:3:{i:0;s:7:\"museums\";i:1;s:10:\"coffee-tea\";i:2;s:13:\"art-galleries\";}"], "_links"=>{"self"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/businesses/1745"}], "collection"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/businesses"}], "about"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/types/business"}], "author"=>[{"embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/users/1"}], "replies"=>[{"embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/comments?post=1745"}], "https://api.w.org/attachment"=>[{"href"=>"http://highstreetbeacons.com/wp-json/wp/v2/media?parent=1745"}], "https://api.w.org/term"=>[{"taxonomy"=>"listing_categories", "embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/listing_categories?post=1745"}, {"taxonomy"=>"locations", "embeddable"=>true, "href"=>"http://highstreetbeacons.com/wp-json/wp/v2/locations?post=1745"}]}}

我刚刚创建了一个插件来注册自定义字段,而不是依赖仅包含一个字段的 REST API Enabler 插件来查看是否可以使其正常工作

<?php
/**
* Plugin Name: Jamesc Register API Fields
* Plugin URI: http://danielpataki.com
* Description: This plugin exposes business meta data mfields to REST API.
* Version: 1.0.0
* Author: Smiler
*/


/**
* Use arbitrary functions to add a field
*/
add_action( 'rest_api_init', 'register_something_random' );
function slug_register_something_random() {
register_rest_field( 'business',
'listing_address',
array(
'get_callback' => 'get_meta_field',
'update_callback' => 'update_meta_field',
'schema' => null,
)
);
}

/**
* Handler for getting custom field data.
*
* @since 0.1.0
*
* @param array $object The object from the response
* @param string $field_name Name of field
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function get_meta_field( $object, $field_name, $request ) {
return get_post_meta( $object[ 'id' ], $field_name );
}

/**
* Handler for updating custom field data.
*
* @since 0.1.0
*
* @param mixed $value The value of the field
* @param object $object The object from the response
* @param string $field_name Name of field
*
* @return bool|int
*/
function update_meta_field( $value, $object, $field_name ) {
if ( ! $value || ! is_string( $value ) ) {
return;
}

return update_post_meta( $object->ID, $field_name, strip_tags( $value ) );

}

但这没有任何效果。

我现在完全陷入困境,尝试了我能想到的一切。

最佳答案

我的 WordPress 插件存在一个明显的问题,其中添加操作函数名称与实际函数名称不匹配register_something_randomslug_register_something_random()

add_action( 'rest_api_init', 'register_something_random' );
function slug_register_something_random() {

应该是

add_action( 'rest_api_init', 'register_something_random' );
function register_something_random() {

进行了此更改,现在一切正常

关于wordpress - 如何通过 WP REST API V2 使用自定义字段正确创建自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519680/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com