'How do you add custom fields defined in posts to the rest API responses in wordpress

I want to do this without using any sort of plugin since these are both core wordpress features (custom fields and the REST API). Here is the documentation for custom fields for reference:

https://codex.wordpress.org/Using_Custom_Fields

Here is a screenshot from my wordpress installation:

wordpress custom fields

Here is what the API response for a post looks like currently:

{
    "_links": {
        "about": [
            {
                "href": "http://example.com/wp-json/wp/v2/types/post"
            }
        ],
        "author": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/users/1"
            }
        ],
        "collection": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts"
            }
        ],
        "curies": [
            {
                "href": "https://api.w.org/{rel}",
                "name": "wp",
                "templated": true
            }
        ],
        "replies": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/comments?post=21"
            }
        ],
        "self": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21"
            }
        ],
        "version-history": [
            {
                "href": "http://example.com/wp-json/wp/v2/posts/21/revisions"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://example.com/wp-json/wp/v2/media?parent=21"
            }
        ],
        "wp:featuredmedia": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/media/23"
            }
        ],
        "wp:term": [
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/categories?post=21",
                "taxonomy": "category"
            },
            {
                "embeddable": true,
                "href": "http://example.com/wp-json/wp/v2/tags?post=21",
                "taxonomy": "post_tag"
            }
        ]
    },
    "author": 1,
    "categories": [
        5,
        4
    ],
    "comment_status": "open",
    "content": {
        "protected": false,
        "rendered": ""
    },
    "date": "2017-05-14T15:25:33",
    "date_gmt": "2017-05-14T15:25:33",
    "excerpt": {
        "protected": false,
        "rendered": ""
    },
    "featured_media": 23,
    "format": "standard",
    "guid": {
        "rendered": "http://example.com/?p=21"
    },
    "id": 21,
    "link": "http://example.com/2017/05/14/post/",
    "meta": [],
    "modified": "2017-05-15T18:17:34",
    "modified_gmt": "2017-05-15T18:17:34",
    "ping_status": "open",
    "slug": "",
    "sticky": false,
    "tags": [],
    "template": "",
    "title": {
        "rendered": ""
    },
    "type": "post"
}

In case it could be relevant, here are my active plugins:

list of active wordpress plugins: Enable Media Replace, S3 Uploads



Solution 1:[1]

First you need to register_rest_fields to adding custom endpoints in WP REST API JSON Response

add_action( 'rest_api_init', 'add_custom_fields' );
function add_custom_fields() {
register_rest_field(
'post', 
'custom_fields', //New Field Name in JSON RESPONSEs
array(
    'get_callback'    => 'get_custom_fields', // custom function name 
    'update_callback' => null,
    'schema'          => null,
     )
);
}

Then define your functions to get custom fields

function get_custom_fields( $object, $field_name, $request ) {
//your code goes here
return $customfieldvalue;
}

Tested on local site

enter image description here

Solution 2:[2]

I found the REST API Custom Fields plugin useful for this.

Solution 3:[3]

Use this plugin for this solution: https://wordpress.org/plugins/acf-to-rest-api/ as it allows you to expose the Advanced Custom Fields into their own JSON response via new endpoints listed here https://github.com/airesvsg/acf-to-rest-api#endpoints. You can then forkJoin the two observables (if using Angular) and utilize the combined response toward your purpose.

Solution 4:[4]

Suppose we want to add 'sku' and 'price' meta to the 'product' postType and the rest_api url is :

site_addresses.com/wp-json/wp/v2/product

Put this code in the functions.php

function af_create_custom_meta_in_REST()
{
    $post_types = ["product"]; //post types that will include custom fields
    foreach ($post_types as $post_type) {
        register_rest_field(
            $post_type,
            'custom_fields',
            [
                'get_callback'    => 'expose_custom_fields',
                'schema'          => null,
            ]
        );
    }
}
function expose_custom_fields($object)
{
    $fields = ["_sku", "_price"]; //custom fields name
    $post_ID = $object['id'];
    $return_value = [];
    foreach ($fields as $field) {
        $field = strval($field);
        $meta = get_post_meta($post_ID, $field);
        if (count($meta) > 1)
            $return_value[$field] = $meta;
        else if ($meta[0])
            $return_value[$field] = $meta[0];
        else
            $return_value[$field] = ""; //if you set this to null result will be 'null'
    }
    if ($return_value)
        return $return_value;
    return []; //if you set this to null result will be 'null'
}
add_action('rest_api_init', 'af_create_custom_meta_in_REST');

you can change $post_types or $fields to anything you want

Result image : image of rest_api reslut

Solution 5:[5]

For those using Advanced Custom Fields: this is now offered out of the box as of v5.11 on a per-field group basis. Steps:

  1. Edit the field group you want added to the REST API
  2. Under Settings, click Show in REST API

For fine-grain control over what exactly the REST API shows and when, see Documentation > Guides > WP REST API Integration on the ACF website. This includes examples of how to include/exclude individual fields within a field group.

Solution 6:[6]

Just add it in your CMS and then it should be available inside your WP REST API.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rheeantz
Solution 2 cbp
Solution 3
Solution 4 AliAfzali
Solution 5 kwaves
Solution 6 Bruce Chamoff