1

I'm working on a custom WordPress project where I’ve registered a custom post type called parcel. It has several custom meta fields like pickup_pincode, drop_pincode, and delivery_type.

I want to expose these custom fields via the WordPress REST API when fetching the parcel posts (e.g., /wp-json/wp/v2/parcel).

I’ve registered the custom post type using register_post_type, and the meta fields using register_post_meta.

Here’s what I’ve tried in functions.php:

function register_parcel_meta_fields() {
    register_post_meta('parcel', 'pickup_pincode', [
        'show_in_rest' => true,
        'type'         => 'string',
        'single'       => true,
    ]);
}
add_action('init', 'register_parcel_meta_fields');

I registered the meta fields using register_post_meta() with 'show_in_rest' => true, expecting them to appear automatically in the REST API response for my custom post type parcel. However, when I fetch the data via /wp-json/wp/v2/parcel, the custom fields like pickup_pincode are missing. I expected them to be visible in the JSON response but they’re not showing up. I’m unsure if I need to do more or hook into a different filter.

1
  • It's strange, your code works on my test site. edit your question to add the code you use to register the custom post type. Commented Jul 8 at 15:41

1 Answer 1

0

If the custom meta fields aren't showing in the REST API response, there are a few key things to check and potentially adjust.

1. Make sure your custom post type is set up correctly for REST

When registering the post type with register_post_type(), make sure you’ve included:

'show_in_rest'=> true,

Example:

register_post_type('parcel', [ 'label' => 'Parcels', 'public' => true, 'show_in_rest' => true, // THIS is important 'supports' => ['title', 'editor', 'custom-fields'], // other args... ]);


2. Ensure Meta Fields Are Registered on rest_api_init

Instead of init, use the rest_api_init hook when calling register_post_meta(). This ensures the fields are properly registered when the REST API is initialized:

functionregister_parcel_meta_fields() { register_post_meta('parcel', 'pickup_pincode', [ 'show_in_rest' => true, 'type' => 'string', 'single' => true, ]); register_post_meta('parcel', 'drop_pincode', [ 'show_in_rest' => true, 'type' => 'string', 'single' => true, ]); register_post_meta('parcel', 'delivery_type', [ 'show_in_rest' => true, 'type' => 'string', 'single' => true, ]); } add_action('rest_api_init', 'register_parcel_meta_fields');



3. Check Response Format

When you fetch /wp-json/wp/v2/parcel, scroll to the meta object in the JSON response. The custom fields appear there, like so:

"meta": { "pickup_pincode": "123456", "drop_pincode": "654321", "delivery_type": "express" }

If you don't see the meta key at all:

  • Ensure you're using register_post_meta() as shown.

  • Double-check there's actually meta data saved for those fields on the post.

  • Try adding 'auth_callback' => '__return_true' to the meta registration temporarily for testing:

register_post_meta('parcel', 'pickup_pincode', [ 'show_in_rest' => true, 'type' => 'string', 'single' => true, 'auth_callback' => '__return_true', // for testing ]);



Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.