How do you add custom fields defined in posts to the rest API responses in wordpress
Asked Answered
L

5

15

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

Lighthouse answered 15/5, 2017 at 18:32 Comment(1)
I found the REST API Custom Fields plugin useful for this.Periotic
A
29

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

Apologist answered 16/5, 2017 at 17:23 Comment(4)
Thanks, that makes sense. Where is this code supposed to go?Lighthouse
in your theme function or if you use plugin in your plugin functionApologist
sorry, but how how do I add a value to "custom_fields"Apprehensible
Good answer. This is modifying the response (register_rest_field) rather than adding a custom endpoint (register_rest_route).Curley
L
4

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.

Lindeman answered 27/5, 2022 at 16:59 Comment(0)
P
1

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

Patrimony answered 3/5, 2022 at 15:34 Comment(0)
D
0

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.

Dilatory answered 29/4, 2019 at 12:21 Comment(1)
@PaulRoub I adjusted my answer as requestedDilatory
U
-2

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

Upraise answered 20/1, 2019 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.