GiovanniManetti11
GiovanniManetti11

Reputation: 46

Wordpress custom fields don't show as meta in JSON response

So I have this WP headless where I get API, I have added some custom fields (Wordpress built in) but it seems they don't show as meta_key and meta_value. Meta field in JSON response is empty. Here is my JSON response and custom fields are called "featured", "iframe1", "iframe2". How can I show them as meta?

Upvotes: 1

Views: 1105

Answers (1)

GiovanniManetti11
GiovanniManetti11

Reputation: 46

I just found the answer myself: I need to register the meta fields first. The function to use it register_meta(). So I added to my functions.php

add_action( 'rest_api_init', 'register_meta_fields');
function register_meta_fields(){

register_meta( 'post', 'featured', array(
    'type' => 'string',
    'description' => 'featured post',
    'single' => true,
    'show_in_rest' => true
));

register_meta( 'post', 'iframe1', array(
    'type' => 'string',
    'description' => 'iframe1',
    'single' => true,
    'show_in_rest' => true
));

register_meta( 'post', 'iframe2', array(
    'type' => 'string',
    'description' => 'iframe2',
    'single' => true,
    'show_in_rest' => true
));

register_meta( 'post', 'iframe3', array(
    'type' => 'string',
    'description' => 'iframe3',
    'single' => true,
    'show_in_rest' => true
));

}

Upvotes: 2

Related Questions