0

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' );
  • I've also tried to add a comma in between the usernames in the first shortcode to output "shop####,shop####", and it produced the same result of "No shops found". – John Smithly Jun 12 at 20:55
  • your loop will only set $prof_id once repeatedly rather than create an array of values. so you are just ending up with the last value. – majick Jun 13 at 1:13
  • try $prof_ids = array(); foreach ($usernames as $prof_id) {$prof_ids[] = get_data_by('user_login', 'id');} ... then use $prof_ids insead of array($prof_id); as the include argument. – majick Jun 13 at 1:15
  • Thank you for helping me. I tried what you said, but now it just shows all of the users. Am I supposed to put something in the parenthesis of $prof_ids = array(); ? Or in the brackets of {$prof_ids[] ? – John Smithly Jun 13 at 14:00
  • no that is how it goes, the first just sets an empty array, the second adds to the next index starting at 0... if you are getting all users then you need to look earlier in your code as to why. – majick Jun 13 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.