• Resolved jan4971

    (@jan4971)


    Hey guys, i have some issues with this thing here:

    add_shortcode('authors-list', 'my_authors_list_shortcode');
    function my_authors_list_shortcode( $atts = array() ) {
        global $wpdb;
        $users = $wpdb->get_results( "SELECT ID, display_name FROM {$wpdb->users}" );
    
        $content = "<ul class='authors-list'>";
        foreach( $users as $user ) {
            $content .= "<li>";
            $content .= get_avatar( $user->ID, 70 );
            $content .= "<h3>" . $user->display_name . "</h3>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'description', true ) . "</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'phone', true ) . "</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'livingplace', true ) . "</p>";
    		$content .= "<p class='author-description'>" . $current_user->user_email . "</p>";
    		$content .= "</li>";
        }
        $content .= "</ul>";
        return $content;
    }

    It seems like there is no way to output all users email adresses.
    Any one got an idea ?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    Your code is currently only getting user ID and display_name from the DB, it also should be getting ‘user_email’ so it can be output. Alternately, select the whole row with ‘*’

    Thread Starter jan4971

    (@jan4971)

    thanks for helping me. ??
    I tried to output mail adresses like this:

    $content .= "<p class='author-description'>" . $current_user->user_email . "</p>";

    But it didnt work.
    I just want some details and not everthing.

    EDIT:

    $users = $wpdb->get_results( "SELECT ID, display_name FROM {$wpdb->users}" );

    I will try to change this line to:

    $users = $wpdb->get_results( "SELECT * FROM {$wpdb->users}" );

    Will report if it works.

    Thread Starter jan4971

    (@jan4971)

    Your code is currently only getting user ID and display_name from the DB

    Now I came up with this:

    add_shortcode('authors-list', 'my_authors_list_shortcode');
    function my_authors_list_shortcode( $atts = array() ) {
        global $wpdb;
    $users = $wpdb->get_results( "SELECT * FROM {$wpdb->users}" );
    
        $content = "<ul class='authors-list'>";
        foreach( $users as $user ) {
            $content .= "<li>";
            $content .= get_avatar( $user->ID, 70 );
            $content .= "<h3>" . $user->display_name . "</h3>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'user_mail', true ) . "</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'phone', true ) . "</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'mobil', true ) . "</p>";
    		$content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'street', true ) . "</p>";
    		$content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'livingplace', true ) . "</p>";
    		$content .= "</li>";
        }
        $content .= "</ul>";
        return $content;
    }

    Everything is getting put out, except for mail.

    Moderator bcworkz

    (@bcworkz)

    Using SELECT * gets us closer to a solution.

    The default reference for user email within your code’s context is $user->user_email. Unless you have some custom code storing it in user meta, user meta is the wrong place to try to get it from.

    In other words, try this line:
    $content .= "<p class='author-description'>{$user->user_email}</p>";

    Thread Starter jan4971

    (@jan4971)

    In other words, try this line:
    $content .= “<p class=’author-description’>{$user->user_email}</p>”;

    That’s the full working code for everyone who’s interested in making something similar.

    function my_authors_list_shortcode( $atts = array() ) {
        global $wpdb;
    $users = $wpdb->get_results( "SELECT * FROM {$wpdb->users}" );
    $user_info = get_userdata($user->ID);
    
        $content = "<ul class='authors-list'>";
        foreach( $users as $user ) {
            $content .= "<li>";
            $content .= get_avatar( $user->ID, 70 );
            $content .= "<h3>" . get_user_meta( $user->ID, 'last_name', true ) . "";
            $content .= " " . get_user_meta( $user->ID, 'first_name', true ) . "</h3>";
    		$content .= "<p class='author-description'>{$user->user_email}</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'phone', true ) . "</p>";
            $content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'mobil', true ) . "</p>";
    		$content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'street', true ) . "</p>";
    		$content .= "<p class='author-description'>" . get_user_meta( $user->ID, 'livingplace', true ) . "</p>";
    		$content .= "</li>";
        }
        $content .= "</ul>";
        return $content;
    }

    That’s fantastic! Thanks for helping me out !

    Is there a way to sort the output ascending with the last name to make a List from A-Z ?

    EDIT: I almost figured it out by myself:

    $users = $wpdb->get_results( "SELECT * FROM {$wpdb->users} ORDER BY display_name" );

    I still cant sort by last_name…
    Could you please give me a last push ? ??

    Moderator bcworkz

    (@bcworkz)

    I’m afraid the query gets a fair bit more complicated to sort by last name, because it is not in the posts table. You need to join in the user_meta table, which is beyond my meager SQL skills.

    I usually resort to making a query with WP_Query just to have a base query from which to work. You can grab the full query from the ‘posts_request’ filter. From that base query, I can usually figure out how to customize it, though frequently with many missteps along the way.

    Another possibility is to sort with PHP after it is returned by the query. It’s not nearly as efficient, but depending on the application, it may not matter much.

    Thread Starter jan4971

    (@jan4971)

    Thank you very much for your help @bcworkz.

    I mark it as resolved.

    I will report if i found an solution for the last_name sorting problem.

    Thread Starter jan4971

    (@jan4971)

    I kinda solved it:

    I just added some custom fields to the registration form and used this nice little thing thing to set the display name as “last_name first_name”.

    So i keep sorting by display name which is now “last_name first_name”.

    I know, it’s not perfect, but it’s doing the job.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Problem with user_mail in functions.php’ is closed to new replies.