0

When I make a rest request for a post, I dont get any custom fields (meta). Is there a REST request or parameter I can use that will give me the meta information aswell as all the post information?

  • On default are custom meta data are not inside the REST API, it is necessary to add them via register_rest_field(). – bueltge Apr 19 '18 at 12:55
  • @bueltge thanks for your reply. Do you mean I will need to register my own rest route? Something like /wp-json/wp/v2/meta? And then I will need to make 2 rest calls; 1 for the post ( /wp-json/wp/v2/posts/2) and 1 for the post meta ( /wp-json/wp/v2/meta/2)? – sazr Apr 19 '18 at 13:01
  • I think this is the thread there helps you wordpress.stackexchange.com/questions/227506/… – bueltge Apr 19 '18 at 13:04
0

Like bueltge alluded, you need to register custom meta fields and expose to to the api. I recently had this problem when I was trying to get a custom meta field for our users. I think my solution will work for you if you adapt it for posts vs users (pay attention to the Register new field section). The codex docs were very helpful, too.

// Extends user profiles with building_id field
function fb_add_custom_user_profile_fields( $user ) {
?>
    <table class="form-table">
        <tr>
            <th>
                <label for="building_id"><?php _e('Building ID', 'your_textdomain'); ?>
            </label></th>
            <td>
                <input type="text" name="building_id" id="building_id" value="<?php echo esc_attr( get_the_author_meta( 'building_id', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e('Please enter your building_id.', 'your_textdomain'); ?></span>
            </td>
        </tr>
    </table>
<?php }

function fb_save_custom_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) )
        return FALSE;
    update_usermeta( $user_id, 'building_id', $_POST['building_id'] );
}

add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

// Register new field (building _id) and expose it to WP REST API
// See docs:  https://developer.wordpress.org/reference/functions/register_rest_field/
add_action( 'rest_api_init', 'create_api_users_meta_field' );

function create_api_users_meta_field() {
    // Codex format example: register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
    register_rest_field( 'user', 'buidling_id', array(
           'get_callback'    => 'get_user_meta_for_api',
           'schema'          => null,
        )
    );
}

function get_user_meta_for_api( $object ) {
    //get the id of the user object array
    $user_id = $object['id'];
    //return the user meta
    return get_user_meta( $user_id );
}

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.