REST Request Post including metacustom fields

admin2025-01-07  7

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?

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?

Share Improve this question asked Apr 19, 2018 at 12:21 sazrsazr 3572 gold badges7 silver badges14 bronze badges 3
  • On default are custom meta data are not inside the REST API, it is necessary to add them via register_rest_field(). – bueltge Commented Apr 19, 2018 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 Commented Apr 19, 2018 at 13:01
  • I think this is the thread there helps you wordpress.stackexchange.com/questions/227506/… – bueltge Commented Apr 19, 2018 at 13:04
Add a comment  | 

1 Answer 1

Reset to default 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 );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259500a593.html

最新回复(0)