How to Include Custom Meta Fields in WordPress REST API for Custom Post Types

How to Include Custom Meta Fields in WordPress REST API for Custom Post Types featured image

Reading Time: 2 min, 27 sec

When you create a custom post type in WordPress, you often add custom meta fields to store additional information, like extra details, custom settings, or unique attributes for each post. However, these meta fields are not included in the REST API response by default, which means they won’t be accessible when you fetch posts via the API. To make them available, you need to register the meta fields properly so that the REST API can expose them.

Here’s how you can do it.

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider

1. Register Your Custom Post Type

First, make sure your custom post type is registered and supports the REST API. For example:

function my_custom_post_type() {
    $args = array(
        'label' => 'Products',
        'public' => true,
        'show_in_rest' => true, // Enable REST API
        'supports' => array('title', 'editor', 'custom-fields')
    );
    register_post_type('product', $args);
}
add_action('init', 'my_custom_post_type');

Key point: The 'show_in_rest' => true parameter is necessary to make the post type available in the REST API.

2. Register Meta Fields for REST API

Next, register your custom meta fields so they are exposed in the REST API. You can use register_post_meta() or register_meta(). Here’s an example:

function register_product_meta() {
    register_post_meta('product', 'price', array(
        'show_in_rest' => true, // Make it available in REST API
        'single' => true,
        'type' => 'number'
    ));

    register_post_meta('product', 'sku', array(
        'show_in_rest' => true,
        'single' => true,
        'type' => 'string'
    ));
}
add_action('init', 'register_product_meta');

Explanation:

  • 'product' is the post type.
  • 'price' and 'sku' are meta keys.
  • 'show_in_rest' => true makes them appear in the REST API response.
  • 'single' => true means the meta field holds a single value, not an array.
  • 'type' defines the data type (string, number, boolean, etc.).

3. How It Appears in REST API

After adding the code above, if you query your custom post type endpoint, the meta fields appear under meta:

Endpoint:

https://yoursite.com/wp-json/wp/v2/product

Example Response:

{
    "id": 123,
    "date": "2025-09-19T10:00:00",
    "title": { "rendered": "Product Name" },
    "meta": {
        "price": 499,
        "sku": "PROD001"
    }
}

4. Optional: Register Meta Directly with show_in_rest Arguments

For more advanced control, you can register meta with rest_schema to define validation rules, description, or visibility in REST API:

register_post_meta('product', 'price', array(
    'show_in_rest' => array(
        'schema' => array(
            'type' => 'number',
            'description' => 'Product price in INR',
            'default' => 0
        )
    ),
    'single' => true
));

You should always enable custom-fields support in your post type if you want meta fields. However, if you are exposing sensitive data, use the auth_callback argument to control access. You should also use descriptive meta keys to avoid conflicts with plugins or other fields.

Conclusion

Exposing custom meta fields in the WordPress REST API is straightforward once your custom post type and meta are registered correctly. By using 'show_in_rest' => true and proper data types, your meta fields appear cleanly in API responses, ready for use in themes, plugins, or headless WordPress setups.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services