WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] user meta and a user page loop (10 posts)

  1. ggarmento
    Member
    Posted 2 years ago #

    My lack of <?PHP> skills are showing. I am trying to list my authors with user_levels of 2 (author) and 7 (editor) on a page. I have crawled the CODEX and learned that user_login (in wp_users) and user_level (in wp_usermeta) are in different tables.

    I know this much:
    do a QUERY of the wordpress db and JOIN the wp_users and wp_usermeta tables WHERE user_level is equal to 2 and 7.

    But that syntax eludes me

    this code works in a static page, but not in a loop.

    User Name: <?php echo get_the_author_meta('user_login', 3); ?>
    User Level: <?php echo get_the_author_meta('user_level', 3); ?>

    any clues?

  2. ggarmento
    Member
    Posted 2 years ago #

    Here is my next run at the wall... I was PHP coding about five years ago, and this code syntax may show it.

    http://pastebin.com/FNCekLf6

    I was able to return data from the db wp_users table, and populate the HTML fields below, but the structure of the db wp_usermeta table is weird. So the table JOIN breaks at the following line.
    while ($usersarray = mysql_fetch_array($usersselect)) {

    any help would go a long way.

  3. Andrei Olsen
    Member
    Posted 2 years ago #

    If I understood you correctly you wanted a list of all the authors and editors, in which case you were looking at the wrong function.

    I think get_users is what you were looking for:

    <?php
    if ($editors = get_users( 'orderby=nicename&role=editor' )) {
        echo '<h4>Editors</h4>';
        echo '<ul>';
        foreach ($editors as $user) {
            echo '<li>' . $user->user_nicename . '</li>';
        }
        echo '</ul>';
    }
    
    if ($authors = get_users( 'orderby=nicename&role=author' )) {
        echo '<h4>Authors</h4>';
        echo '<ul>';
        foreach ($authors as $user) {
            echo '<li>' . $user->user_nicename . '</li>';
        }
        echo '</ul>';
    }
    ?>
  4. ggarmento
    Member
    Posted 2 years ago #

    the CODEX says that get_user function is depreciated.

    This code works, it runs a loop and delivers all users only once in teh return. http://pastebin.com/LwNw1CPs

    But it only returns half of the info (from the wp_users table); when I try to JOIN the wp_usermeta table it fails.

    Also, if anyone could stear me in the right direction so I can make good WP DB query requests I would appreciate it.

  5. Andrei Olsen
    Member
    Posted 2 years ago #

  6. ggarmento
    Member
    Posted 2 years ago #

    wow... I don't know what I was looking at earlier.

    the CODEX says that get_user function is depreciated.

    i am goping with "lack of sleep" as an excuse. Thanks I will take tray again.

  7. ggarmento
    Member
    Posted 2 years ago #

    Thanks for your response Andrei.
    Okay that helped in a big way. That resolved my SORT and EXCLUDE issues. The only hurdle now is: getting the meta_values and the meta_keys using the get_users function. The SYNTAX on that part eludes me.
    http://pastebin.com/7MnmH2U4

    The CODEX looks light in that regard. Trying to ask the right question in the search field.

  8. Andrei Olsen
    Member
    Posted 2 years ago #

    You need to add fields parameter.

    $blogusers = get_users(array(
        'blog_id' => 1,
        'orderby' => 'blog_id',
        'exclude' => 1,
        'fields'  => 'all_with_meta'
    ));

    And then inside foreach add (to add the avatar)

    $user_avatar = get_avatar($user->ID, 32);
    echo '<a href="' . $user->user_url . '" target="_blank">' . $user_avatar . '</a>';

    the rest will work as it is now.

  9. ggarmento
    Member
    Posted 2 years ago #

    CheckMate!!! Andrei you are the smartest Norwegian I know.
    Your guidance and help is very much appreciated.
    http://www.chessgames.com/perl/chessgame?gid=1243022

  10. Andrei Olsen
    Member
    Posted 2 years ago #

    Hehe! Nice game ;) and no problem, really.

Topic Closed

This topic has been closed to new replies.

About this Topic