Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I add custom fields to a WordPress REST API response?
Asked on Jan 20, 2026
Answer
To add custom fields to a WordPress REST API response, you can use the `register_rest_field` function. This allows you to include additional data in the API response for a specific post type.
<!-- BEGIN COPY / PASTE -->
function add_custom_fields_to_rest_api() {
register_rest_field('post', 'custom_field', array(
'get_callback' => function($object) {
return get_post_meta($object['id'], 'custom_field_key', true);
},
'schema' => array(
'description' => 'Custom field added to REST API response',
'type' => 'string',
),
));
}
add_action('rest_api_init', 'add_custom_fields_to_rest_api');
<!-- END COPY / PASTE -->Additional Comment:
- Replace "post" with your desired post type if necessary.
- "custom_field_key" should be replaced with the actual meta key of your custom field.
- This code should be placed in your theme's functions.php file or a custom plugin.
- Ensure that the REST API is enabled for the post type you are modifying.
Recommended Links:
