Outputting user data in group loop

Support MB Group Outputting user data in group loop

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39081
    YasmineYasmine
    Participant

    Hello,

    I am trying to output user data (avatar and display name) in an elementor meta-box group. In my group I have it set for multiple users to be selected, but no clone.

    I have created a meta box group field. Then I have put this into a loop, which I then want to show in a post archive. So that I can output the associated users of that post.

    1. Would this be a recommended appraoch?
    2. I made a shortcode with a "foreach" statement that I output in the MB group, but as it is multiple, not cloneable, am I going wrong with my array?

    add_shortcode("former_team_member_get_name", function (){
    	global $post;
    	$ftm_post_type = get_post_type($post->ID);
    	if(is_singular() && "team" == $ftm_post_type)
    	{
    		$ftmn_field_id = rwmb_meta( "archived_team_users_group",'',$post->ID); 
    	foreach($ftmn_field_id as $user){
    		$ftmn_user_name = get_userdata(intval($user['former_team_member']))->display_name;
    		return $ftmn_user_name;

    I am really stuck with this, and have no idea even at what point I am going wrong. Or what documentation to use.

    Your help would be hugely appreciated.
    Yasmine

    #39098
    PeterPeter
    Moderator

    Hello Yasmine,

    Can you please share the code that creates the custom fields on your site? I recommend using this code to output a variable if you do not know what could be the value.

    echo "<pre>";
    print_r( $variable );
    echo "</pre>";
    #39109
    YasmineYasmine
    Participant

    Hi Peter,

    Yes of course:

           $prefix = '';
    
    'name'   => __( 'Archived Users', 'your-text-domain' ),
                    'id'     => $prefix . 'archived_team_users_group',
                    'type'   => 'group',
                    'fields' => [
                        [
                            'name'            => __( 'Former team members', 'your-text-domain' ),
                            'id'              => $prefix . 'former_team_member',
                            'type'            => 'user',
                            'field_type'      => 'checkbox_list',
                            'select_all_none' => true,
                            'query_args'      => [
                                'role' => 'archive_team',
                            ],
                        ],
    #39111
    YasmineYasmine
    Participant

    I tried with the print_r but did not fix it.

    I also found that if I use the shortcode directly on the template, it will show the first value. But using it via the meta box group widget shows nothing

    #39118
    YasmineYasmine
    Participant

    Update:
    Hi Peter,

    With the following code, I have got the avatar and photo to show within the metabox group on elementor. The first value correctly populates the name and avatar.

    //For display_name
    add_shortcode("former_team_member_get_name", function (){
    	global $post;
    $ftm_post_type = get_post_type($post->ID); 
    if(is_singular() && "team" == $ftm_post_type)
    {
    $group_id = rwmb_meta( 'archived_team_users_group','',$post->ID); 
    foreach ($group_id as $x){
    $f_user_id = $group_id['former_team_member'] ?? '';
    foreach ($f_user_id as $user){
    	$ff_user = get_usermeta($user);
    }
    		return sprintf($ff_user[1]);
    		}}});
    
    //For the avatar
    add_shortcode("former_team_member_get_avatar", function (){
    	global $post;
    $ftm_post_type = get_post_type($post->ID); 
    if(is_singular() && "team" == $ftm_post_type)
    {
    $group_id = rwmb_meta( 'archived_team_users_group','',$post->ID); 
    foreach ($group_id as $x){
    $f_user_id = $group_id['former_team_member'] ?? '';
    foreach ($f_user_id as $user){
    $profile_pic = get_avatar_url($user);
    $main_html_markup = "<img src='$profile_pic'>";
    }}
    return $main_html_markup;	
    
    		}});

    However, when I try to use the metabox group field within a template that has values, it shows as blank.

    How do I populate the metabox group correctly with shortcodes that then loop to show all the post values on a template?

    #39129
    PeterPeter
    Moderator

    Hello Yasmine,

    From the line to get the group value

    $group_id = rwmb_meta( 'archived_team_users_group','', $post->ID );

    you can replace the variable $post->ID with a real post ID like 123 and see if it works. If yes, there is a problem when getting the post ID above that line. If not, there is a problem when outputting the subfield value. Does that make sense?

    And the function print_r() or var_dump() helps you to output the field value for debugging, it's not a solution.

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