MetaBox user field - how to show current value when query args exclude it

Support General MetaBox user field - how to show current value when query args exclude itResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #18310
    realj42realj42
    Participant

    To explain - I have set up a membership post-type to hold membership details for WordPress Users. Naturally the membership post has a 'user_id' post meta. And users have a 'membership_id' user-meta. In the Membership meta-box there is a user field with select-type 'select advanced'. Now I tried to make things easier for the admin when adding a new membership by restricting the list of user ids shown to only those who have no membership already. Ok that works fine, simplifies adding a new members. The problem is when editing a membership then the user-name is not shown at all - of course the user now has a membership so is excluded by the query, and cannot be selected. When the post is saved, the user-id is removed from the membership post meta! This happens even if you do not try to update the user.

    My current code below. So my question now is - is there any way to include current value of the user-id field on meta-box edit - I would not expect the user id to change once a membership is set up so maybe it can be set to read-only for Edit?
    Thanks for any guidance on this

                'fields' => array(
                    array(
                        'name'  => 'Member',
                        'desc'  => 'The user holding this membership - must already exist',
                        'id'    => SHS_POST_META_PREFIX . 'user_id',
                        'type'  => 'user',
                        'field_type'  => 'select_advanced',
                        'placeholder' => 'Select member',
                        'query_args'  => array(
                            'meta_query' => array(
                                'relation' => 'or',
                                array(
                                    'key' => SHS_POST_META_PREFIX . 'membership_id',
                                    'value' => 0,
                                    'compare' => 'NOT EXISTS',
                                    'type' => 'numeric',
                                ),
                                array(
                                    'key' => SHS_POST_META_PREFIX . 'membership_id',
                                    'value' => 0,
                                    'compare' => '=',
                                    'type' => 'numeric',
                                )
                            )
                        ),
                    ),  
    #18319
    Anh TranAnh Tran
    Keymaster

    Hi, I got the problem. The query args inherits all parameters from get_users() function, which has a parameter include that you can use in case of editing a membership.

    This is a pseudo-code I could imagine:

    $include = [];
    if ( $_GET['post'] ) { // Edit a post
        $post_id = $_GET['post'];
        $user_id = get_post_meta( $post_id, 'user_id', true );
        $include[] = $user_id;
    }
    // Then in the query args, just add 'include'
    ..
    'query_args' => [
        'include' => $include,
        ..
    ],
    #18325
    realj42realj42
    Participant

    Hi that is great,thanks. I assume this code should be placed in an rwmb_before() action callback?

    Cheers
    Hywel

    #18337
    Anh TranAnh Tran
    Keymaster

    Hi Hywel,

    It should be placed inside the rwmb_meta_boxes filter, just before your meta box registration code, like this:

    add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
        $include = [];
        if ( $_GET['post'] ) { // Edit a post
            $post_id = $_GET['post'];
            $user_id = get_post_meta( $post_id, 'user_id', true );
            $include[] = $user_id;
        }
    
        $meta_boxes = [
            'title' => 'Test',
            ...
            'fields' => [
                [
                    'name' => 'Member',
                    ...
                    'query_args' => [
                        'include' => $include,
                        ..
                    ]
                ],
            ],
        ];
    } );
    #18343
    realj42realj42
    Participant

    Thanks Anh Tran, should have realised that!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.