【发布时间】:2021-06-11 04:20:51
【问题描述】:
我在我的 WooCommerce 网站上添加了一堆自定义字段 - 它们在产品页面上显示良好,现在我可以通过 API 返回它们,因为我已经添加了下面的代码 - 但我无法更新他们通过 API - 希望我错过了一些非常明显的东西:)
我的代码(使用 PHP Inster 添加):-
function create_ACF_meta_in_REST() {
$postypes_to_exclude = ['acf-field-group','acf-field'];
$extra_postypes_to_include = ["post"];
$post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);
array_push($post_types, $extra_postypes_to_include);
foreach ($post_types as $post_type) {
register_rest_field( $post_type, 'ACF', [
'get_callback' => 'expose_ACF_fields',
'update_callback' => 'update_ACF_fields',
'schema' => null,
]
);
}
}
function update_ACF_fields( $data, $object ) {
$ID = $object['id'];
return update_post_meta( $ID, $data );
}
function expose_ACF_fields( $object ) {
$ID = $object['id'];
return get_fields( $ID );
}
add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );
任何指针将不胜感激
【问题讨论】:
标签: rest woocommerce advanced-custom-fields custom-fields