admin管理员组

文章数量:1386872

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;
}

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;
}
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Mar 15, 2017 at 15:45 Night MongerNight Monger 1012 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

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/

本文标签: post metaMake Custom Fields Public in JSONAPI