Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is a question related to an answer posted here

The above solution outputs a list of a specified Group members' display names. I need to extend it to show 2 user fields and 1 usermeta field. So I have tried to alter the SQL select statement to do 2 left joins with 2 where conditions, however it isn't working.

I am wondering if someone can check my SQL statement and steer me in the right direction please? NB. mobile_phone is a custom usermeta field that I have defined and populated.

add_shortcode( 'groups_group_user_mobiles', 'groups_group_user_mobiles' );

function groups_group_user_mobiles( $atts, $content = null ) {
global $wpdb;
$output = "";
$options = shortcode_atts(
    array(
        'group' => '',
        'show' => '',
        'format' => '',
        'single' => '1',
        'plural' => '%d'
    ),
    $atts
);
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
    $current_group = Groups_Group::read_by_name( $group );
}

if ( $current_group ) {
            $user_group_table = _groups_get_tablename( "user_group" );
            $users = $wpdb->get_results( $wpdb->prepare(
                "SELECT * FROM $wpdb->users,$wpdb->usermeta 
                 LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id
                 LEFT JOIN $user_group_table ON $wpdb->usermeta.user_id = $user_group_table.user_id 
                 WHERE $user_group_table.group_id = %d  
                 AND $wpdb->usermeta.meta_key = 'mobile_phone'",
                 Groups_Utility::id( $current_group->group_id )
            ) );
            if ( $users ) {
                $output .= '<ul>';
                foreach( $users as $user ) {
                    $output .= '<li>' . wp_filter_nohtml_kses( $user->display_name ) ." - ". wp_filter_nohtml_kses( $user->user_email ) ." - ". wp_filter_nohtml_kses( $user->meta_value ) . '</li>';
                }
                $output .= '</ul>';
            }
}
return $output;
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.