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
- This topic has 4 replies, 2 voices, and was last updated 5 years, 2 months ago by
realj42.
-
AuthorPosts
-
February 17, 2020 at 1:43 AM #18310
realj42
ParticipantTo 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', ) ) ), ),
February 17, 2020 at 9:34 PM #18319Anh Tran
KeymasterHi, 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, .. ],
February 17, 2020 at 11:25 PM #18325realj42
ParticipantHi that is great,thanks. I assume this code should be placed in an rwmb_before() action callback?
Cheers
HywelFebruary 19, 2020 at 10:52 AM #18337Anh Tran
KeymasterHi 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, .. ] ], ], ]; } );
February 19, 2020 at 4:48 PM #18343realj42
ParticipantThanks Anh Tran, should have realised that!
-
AuthorPosts
- You must be logged in to reply to this topic.