如何注册文章自定义字段到文章的rest api断点

注册存在的单个字段

function register_domain_meta_for_rest() {
    // Replace 'your_meta_key' with your actual meta field keys
    register_post_meta('domain', 'your_meta_key', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string', // adjust type as needed
        'auth_callback' => function() {
            return current_user_can('edit_posts');
        }
    ));
    
    // Add more meta fields as needed
    register_post_meta('domain', 'another_meta_key', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string',
        'auth_callback' => function() {
            return current_user_can('edit_posts');
        }
    ));
}
add_action('init', 'register_domain_meta_for_rest');

注册所有的字段到rest api

function add_domain_meta_to_rest() {
    register_rest_field('domain', 'meta_fields', array(
        'get_callback' => function($post) {
            $meta = get_post_meta($post['id']);
            $filtered_meta = array();
            
            // Filter out WordPress internal meta (starts with _)
            foreach($meta as $key => $value) {
                if(!str_starts_with($key, '_')) {
                    $filtered_meta[$key] = is_array($value) && count($value) == 1 ? $value[0] : $value;
                }
            }
            
            return $filtered_meta;
        },
        'update_callback' => function($value, $post) {
            foreach($value as $key => $val) {
                update_post_meta($post->ID, $key, $val);
            }
            return true;
        },
        'schema' => array(
            'description' => 'Custom meta fields',
            'type' => 'object'
        )
    ));
}
add_action('rest_api_init', 'add_domain_meta_to_rest');

注册一些特定字段

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)
    });
×
如何注册文章自定义字段到文章的rest api断点

如何注册文章自定义字段到文章的rest api断点

注册存在的单个字段 function r...