• Resolved ggarmento

    (@ggarmento)


    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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter ggarmento

    (@ggarmento)

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

    https://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.

    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>';
    }
    ?>
    Thread Starter ggarmento

    (@ggarmento)

    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. https://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.

    Thread Starter ggarmento

    (@ggarmento)

    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.

    Thread Starter ggarmento

    (@ggarmento)

    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.
    https://pastebin.com/7MnmH2U4

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

    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.

    Thread Starter ggarmento

    (@ggarmento)

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

    Hehe! Nice game ?? and no problem, really.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘user meta and a user page loop’ is closed to new replies.