admin管理员组文章数量:1426096
My code below works to list a single user when I ask to search for a single username; however, I am unable to list multiple users when asking to search for multiple usernames.
Here is a brief overview of what I'm doing even though what I'm trying to accomplish is pretty general and doesn't only apply to my situation. Please excuse my method of using shortcodes within another shortcode as I'm still learning how all this works:
- I am using Ultimate Member Plugin for profiles.
- I have two profiles I'm working with: franchisees and shops.
- The franchisee selects the shops they own from field(s) 'stores' and 'stores_30' in their profile.
- The shop profiles are set up with usernames like 'shop####'.
- The store numbers are converted into usernames with the first shortcode
[get_shop_users meta_key="stores" user_id="#"]
. If multiple stores are selected, this outputs as 'shop####shop####'. - For some reason I could only make the current code work by using another shortcode
[profile_id]
to get the profile ID, otherwise it was grabbing the current user which I don't want. - I expect the shortcode
[test2 meta_key="stores"]
to display a list of those selected stores with their respective information in a table.
Here is the full code: GitHub
What Works
When only 1 store is selected, the do_shortcode()
below outputs "shop####". So it searches all user_login's for that single shop. No problems there. Selecting multiple stores outputs "No shops found".
// WP_User_Query arguments
$args = array(
'search' => do_shortcode(), // Search these usernames
'search_columns' => array( 'user_login' ), // Search by username
'orderby' => 'user_login', // Sort by username
);
// The User Query
$user_query = new WP_User_Query( $args );
What I've Tried
I tried searching for the usernames, converting them into user IDs, and then inputting the user UDs into an 'include'
array, but I'm not sure how to do it. I tried changing the code above to this:
// Get Usernames
$usernames = do_shortcode(); // List of usernames
// Convert Usernames to Profile IDs
foreach ($usernames as $prof_id){
$prof_id = array( get_data_by( 'user_login', 'id' ) );
}
// WP_User_Query arguments
$args = array(
'include' => array( $prof_id ),
);
// The User Query
$user_query = new WP_User_Query( $args );
Which only outputs "No shops found".
EDIT
I have changed the code above to majick's suggestion and included the do_shortcode():
// Get Usernames
$usernames = do_shortcode('[get_shop_users meta_key="' . $meta_key . '" user_id="' . $atts['owner'] . '"]'); // Search these usernames
// Fetch the Users
$prof_ids = array();
foreach ($usernames as $prof_id) {
$prof_ids[] = get_data_by('user_login', 'id');
}
// WP_User_Query arguments
$args = array(
'include' => $prof_ids,
);
// The User Query
$user_query = new WP_User_Query( $args );
I am wondering if the way I went about creating the user names is causing the problem. Any suggestions?
function get_shop_users_shortcode( $atts ) {
$atts = extract( shortcode_atts( array(
'user_id' => um_profile_id(), // Profile ID of franchisee
'meta_key' => '', // Field we will look up shop numbers
), $atts ) );
if ( ! $user_id || empty( $meta_key ) ) return;
$meta_value = get_user_meta( $user_id, $meta_key, true );
if( is_serialized( $meta_value ) ){
$meta_value = unserialize( $meta_value );
}
if( is_array( $meta_value ) ){
$meta_value = implode("shop",$meta_value );
}
return 'shop' . apply_filters("um_user_shortcode_filter__{$meta_key}", $meta_value ) . '';
}
add_shortcode( 'get_shop_users', 'get_shop_users_shortcode' );
本文标签: customizationWPUserQuery Search Multiple Keywords from a MultiSelect Field
版权声明:本文标题:customization - WP_User_Query Search Multiple Keywords from a Multi-Select Field? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745405003a2657210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论