0

How do i make the custom fields public in rest API?

I have added a custom field to the rest api. the challenge is - The metadata is returned when i am signed in my browser. if i try as public using incognito mode - it doesn't return the custom field. I was wondering how do i make it public so that i can access without loggin in. i tried following what is mentioned here

Still not working. Can you let me know what am i missing?

My Full code :

/**
 Changes Related to adding Additional Field "thumbnail_image" in Post 
 */

function addCustomField_register_fields() {
register_rest_field('post',
    'thumbnail_image',
    array(
        'get_callback'      =>'get_custom_field',
        'update_callback'   => null,
        'schema'        => null
    ));

}
function get_custom_field($post, $field_name , $request) {
return get_post_meta($post['id'], 'thumbnail_image', true);
}
add_action('rest_api_init', 'addCustomField_register_fields');

 //* Make the field  public */
add_filter( 'rest_api_allowed_public_metadata', 'allow_thumbnail_metadata' );
function allow_thumbnail_metadata() {
    // only run for REST API requests
    if ( ! defined( 'REST_API_REQUEST' ) || ! REST_API_REQUEST )
        return $allowed_meta_keys;

    $allowed_meta_keys[] = 'thumbnail_image';

    return $allowed_meta_keys;
}
0

Check out this post under the "Get Posts Meta Field Is Not Available By Default" heading: https://1fix.io/blog/2015/07/20/query-vars-wp-api/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.