0

I am trying to grab meta_values from a user profile's multi-select field, and convert them to usernames, which I will then input into an array. Let's say that the user has selected two options (out of hundreds) that equal "4889" and "4982". These need to be converted to usernames "shop4889" and "shop4982" (simply by adding "shop" to the beginning of the selections). The results should be put into my array as follows: $usernames = array('shop4889', 'shop4982'); Here is what I have so far.

/**
 * Create Shortcode for Grabbing Users
 * Usage: [get_shop_users meta_key="stores" user_id="211"]
 */
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' );

This code outputs text as "shop4889, shop4982", but does not work in my array properly. I'm guessing that there is a better way of calling the results?

The array needs to work with the following code, which works fine if I type in the usernames instead of calling them from the user profile.

// Search these Usernames
$usernames = array('shop4889', 'shop4982');

// Fetch the User IDs
$prof_ids = array(); 
foreach ($usernames as $prof_id) {
    $user = get_user_by('login', $prof_id); 
    $prof_ids[] = $user->ID;
}

// WP_User_Query arguments
$args = array(
    'include' => $prof_ids,
);
0

I figured it out...

$meta_value = get_user_meta( $atts['owner'], $meta_key, true );

$store_numbers = array();
foreach ($meta_value as $store_number) {
    $store_numbers[] = 'shop'.$store_number;
}

$usernames = $store_numbers;
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.