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 Mar 06, 2026
Answer
To add custom fields to a WordPress REST API response, you can use the `register_rest_field` function. This function allows you to include additional data in the API response for a specific post type or taxonomy.
<!-- BEGIN COPY / PASTE -->
function add_custom_fields_to_rest_api() {
register_rest_field('post', 'custom_field', array(
'get_callback' => function($post_arr) {
return get_post_meta($post_arr['id'], 'custom_field_key', true);
},
'update_callback' => null,
'schema' => null,
));
}
add_action('rest_api_init', 'add_custom_fields_to_rest_api');
<!-- END COPY / PASTE -->Additional Comment:
- This example adds a custom field to the "post" post type in the REST API response.
- Replace "custom_field_key" with the actual meta key of your custom field.
- The `get_callback` function retrieves the custom field value using `get_post_meta`.
- You can modify the `register_rest_field` parameters to suit your needs, such as adding an `update_callback` for writable fields.
Recommended Links:
