Custom Fields
Re-Envisioned
Support MB User Meta Use MB User Meta data in meta box Reply To: Use MB User Meta data in meta box

#14171

Hello, thanks to help get it works. The concept is reverse. Post get some data from user profile. Changed code to this:

add_action( 'rest_api_init', function () {
    register_rest_field( 'post', 'ag_name', array(
        'get_callback' => function( $user ) {
            $product = get_post_meta( $user['id'], 'the_user', true );
            return get_user_meta( $product, 'ag_name', true );
        },
    ) );
    register_rest_field( 'post', 'ag_address', array(
        'get_callback' => function( $user ) {
            $product = get_post_meta( $user['id'], 'the_user', true );
            return get_user_meta( $product, 'ag_address', true );
        },
    ) );
    register_rest_field( 'post', 'ag_post_code', array(
        'get_callback' => function( $user ) {
            $product = get_post_meta( $user['id'], 'the_user', true );
            return get_user_meta( $product, 'ag_post_code', true );
        },
    ) );
} );

In the metabox field set of the article, I added a filed “the_user” with the author (user field), to identify the user fields, according to the example you have indicated. When the user is indicated, it works correctly, associating the information registered in the profile of the user with the article.

Any possibility of this field or the identification code be automatic? The user field gives not only the user but the list of all users. In json output there is already the variable “author”: 123 so instead of manually set the user, I could use this.

But I do not know how to use it here:
$ product = get_post_meta ($ user [‘id’], ‘the_user‘, true);

Instead of “the_user” use the “author“. The author as the id of user to get the user fields info.

Thanks a lot