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

This topic contains 6 replies, has 2 voices, and was last updated by  designerabreu@gmail.com 1 month, 2 weeks ago.

  • Creator
    Topic
  • #13960
    Resolved
    designerabreu@gmail.com
    Participant

    Hello, is it possible to use the data entered in the MB User Meta in different groups of fields?

    For example, the user posts a custom post with a product and some of the product fields receive information from selected User Meta fields.

    The product meta box is stored in a custom table with all product and user information for use with the REST API.

    If the user publishes several products he avoids having to always be entering information such as name, address, telephone, GPS in each product.

    The project consists of publications of several users who publish products for sale and each product has the contact information with personalized fields. The information is intended to be used in an APP.

    I appreciate the help

    Best regards

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #13979

    Anh Tran
    Keymaster

    Hi,

    I think it’s possible. For example, if you have a select field, you can set the options from current user post meta like this:

    $user_id = get_current_user_id();
    $options = get_user_meta( $user_id, 'meta_key', false ); // false to returns an array
    
    // In select field
    'options' => $options,
    #14145

    Hello, tanks but i am lost.
    I am not a programmer so some easy easy things are not so easy to me
    Where I put the code?

    The pratical case:
    1. I use a set of metaboxes to fill user information (adress, telephone, city, custom email, latitude and longitude).
    Json request give metabox to /wp-json/wp/v2/users/id
    “meta_box”: {
    “ag_name”: “User1”,
    “ag_adress”: “Custom adress”,
    “ag_post_code”: “1234-987”,
    “ag_region”: “Local”,
    “ag_state”: “City”,
    “ag_phone”: “123456789”,

    1. The post has another set of metaboxes with extra info about product.
      Once exists multiples authors, I need add to some fields the data already filled on user profile on the user metabox setup above:

    Example post output needed:

    “meta_box”: {
    //set of fields auto filled with user profile – get data from user metaboxes//

    "ag_name": "User1",
    "ag_adress": "Custom adress",
    "ag_post_code": "1234-987",
    "ag_region": "Local",
    "ag_state": "City",
    "ag_phone": "123456789",
    

    //set of fields filled in the post//

    "to_quantity": "100",
    "to_description": "Custom description",
    "to_advantages": "Custom text",
    

    So json output combine the information of post with information i want show about the author.
    Each post get is author extend author information.

    Thanks for your patience

    #14150

    Anh Tran
    Keymaster

    Hi,

    Now I got it!

    Do you have a field for user to select a product that associated with that user? I didn’t see you mentioned about it above.

    If you already have that, assuming the field ID is ag_product, then you need to modify the rest response to include product details. Here is a sample code that you can use (put it in your theme’s functions.php file or your plugin file):

    add_action( 'rest_api_init', function () {
        register_rest_field( 'user', 'to_quantity', array(
            'get_callback' => function( $user ) {
                $product = get_user_meta( $user['id'], 'ag_product', true );
                return get_post_meta( $product, 'to_quantity', true );
            },
        ) );
        register_rest_field( 'user', 'to_description', array(
            'get_callback' => function( $user ) {
                $product = get_user_meta( $user['id'], 'ag_product', true );
                return get_post_meta( $product, 'to_description', true );
            },
        ) );
        register_rest_field( 'user', 'to_advantages', array(
            'get_callback' => function( $user ) {
                $product = get_user_meta( $user['id'], 'ag_product', true );
                return get_post_meta( $product, 'to_advantages', true );
            },
        ) );
    } );
    #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

    #14177

    Anh Tran
    Keymaster

    I’m not quite clear what do you mean by “Any possibility of this field or the identification code be automatic”. I just understand that posts have a custom field the_user, which stores ID of the user you want to connect with. If you change the field ID from the_user to author, then simply change that text in the code. That’s all.

    #14402

    Works fine, thanks to show the way. Best regards

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.