function add_specific_domain_meta_to_rest() {
// List your specific meta keys here
$meta_keys = array('field1', 'field2', 'field3'); // Replace with your actual meta keys
foreach($meta_keys as $meta_key) {
register_rest_field('domain', $meta_key, array(
'get_callback' => function($post) use ($meta_key) {
return get_post_meta($post['id'], $meta_key, true);
},
'update_callback' => function($value, $post) use ($meta_key) {
return update_post_meta($post->ID, $meta_key, $value);
},
'schema' => array(
'description' => "Meta field: {$meta_key}",
'type' => 'string' // adjust type as needed
)
));
}
}
add_action('rest_api_init', 'add_specific_domain_meta_to_rest');
rset api 使用举例
fetch('http://domain.test/wp-json/wp/v2/domain')
.then(res => res.json())
.then(data => {
console.log(data);
// Meta fields will be in data[0].meta (for Method 1)
// Or data[0].meta_fields (for Method 2)
// Or data[0].field1, data[0].field2 (for Method 3)
});