WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List authors function mysteriously not working (28 posts)

  1. sarahebkaiser
    Member
    Posted 3 years ago #

    I recently upgraded a site I manage, allthingsplc.info/wordpress, to WP3.1. Everything worked except the "wp_list_authors" function, which seems to break no matter where I put it. Right now I want to use it here:

    http://www.allthingsplc.info/wordpress/archives-author.php

    The original code for the page is here:

    http://stikked.com/view/14405892

    I've tried disabling plugins already, if anyone has other ideas for why it's not working they'd be much appreciated. This is so strange - other functions like wp_list_categories work like a charm.

    If I can't get it fixed - can someone tell me how I could just access to the user database and list the authors with their links without using the built-in wordpress function wp_list_authors?

    Thank you!!!

  2. haxxxton
    Member
    Posted 3 years ago #

    im not sure why this isnt working.. but i can tell you how to get a list of the authors..

    get the user ID's of all authors (wp_user_level = 2)

    $author_IDs = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='wp_user_level' AND meta_value='2'");

    get all users to compare to

    $users = $wpdb->get_col("SELECT ID FROM $wpdb->users");

    compare

    for($j=0; $j < count($author_IDs); $j++){
        if(in_array($author_IDs,$users){
          echo $author_IDs[$j];
        }
      }

    not the most elegant coding.. but hey :P

  3. sarahebkaiser
    Member
    Posted 3 years ago #

    You're the best!!!! I think you might have just saved me. I'll try this and let you know if it works.

    The way this one function won't work is pretty weird, right? I wish I had even a vague idea of why it's not working.

  4. haxxxton
    Member
    Posted 3 years ago #

    i havent seen it mentioned as an issue with 3.1 although i dont suppose people use that function much..

    it appears to still be on the 3.1 function list so it hasnt been depreciated.. in fact they added functionality to it by allowing orderby and number

    http://codex.wordpress.org/Function_Reference/wp_list_authors

  5. sarahebkaiser
    Member
    Posted 3 years ago #

    Sorry to be a bit of an idiot, but I tried using the code and now I'm getting a blank page: http://www.allthingsplc.info/wordpress/archives-author.php

    I have a basic understanding of what you're doing but I'm not sure I know where the problem is here - also, if I want to print these author names in li tags, where would I put that?

  6. haxxxton
    Member
    Posted 3 years ago #

    ok if ever you are getting a blank page like that it means there is a php error.. so i may have typed something wrong if you jsut copy pasted..

    to check if thats the case.. pop

    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    at the top of the archive page and it should tell you if you have made a call to an unknown function or something..

    i can actually see where i have gone wrong.. let me ammend my code and ill comment where to add list items around the names the section you want to edit is in the comparison loop:

    $author_IDs = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='wp_user_level' AND meta_value='2'");
    $users = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users");
    for($j=0; $j < count($users); $j++){
        if(in_array($users[$j][ID],$author_IDs){
          //things before the username
          echo $users[$j][user_login];
          //things after the username
        }
      }

    now if you wanted them to be list items youd want to replace my comments with

    echo "<li>";

    and

    echo "</li>";

    respectfully

    sorry for the mess up

    i also forgot that you were going to want the users username not just their ID..

  7. sarahebkaiser
    Member
    Posted 3 years ago #

    Ah, I'm sorry, I tried the new code and it still doesn't seem to be working. Not sure what I'm doing wrong...but I want to say that I really greatly appreciate you helping with this!

    I tried using that function to get error messages but nothing came up.

    Can you tell me where exactly I should put it? I tried literally at the top of the page, like so:

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    ?>

    but that didn't seem to work.

  8. haxxxton
    Member
    Posted 3 years ago #

    oh WOW im silly

    im sorry... here this IS THE CORRECT CODE!! (i not only forgot my closing bracket i also forgot that multi dimentional arrays dont get accessed like that in PHP )

    $author_IDs = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='wp_user_level' AND meta_value='2'");
    $users = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users"); ?>
    <ul>
    <?php
    for($j=0; $j < count($users); $j++){
        if(in_array($users[$j]->ID,$author_IDs0){
          //things before the username
          echo "<li>".$users[$j]->user_login."</li>";
          //things after the username
        }
      }
    ?>
    </ul>

    as a sorry again i put it in a list for you...

    my bad

  9. sarahebkaiser
    Member
    Posted 3 years ago #

    should there be a <?php at the beginning there?

  10. haxxxton
    Member
    Posted 3 years ago #

    yes.. its 1am in Australia.. im sorry

  11. sarahebkaiser
    Member
    Posted 3 years ago #

    Hey, please don't apologize you're being wonderfully kind and helping me! I really appreciate it. It's actually still not working but I'll keep messing with it. I just need to teach myself how all these php/wp functions work and remind myself what a for loop is...

  12. haxxxton
    Member
    Posted 3 years ago #

    oh for the love of god.. i typed a 0 instead of a close bracket...

    <?php
    $author_IDs = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='wp_user_level' AND meta_value='2'");
    $users = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users"); ?>
    <ul>
    <?php
    for($j=0; $j < count($users); $j++){
        if(in_array($users[$j]->ID,$author_IDs)){
          //things before the username
          echo "<li>".$users[$j]->user_login."</li>";
          //things after the username
        }
      }
    ?>
    </ul>

    i have tested this on my own server and it works ok for me :(

    oh and delete that error checking thing from the top of the page now too...

  13. sarahebkaiser
    Member
    Posted 3 years ago #

    This is so exciting! The page now displays but the stuff inside the ul tags isn't getting shown.

    Hm.

  14. haxxxton
    Member
    Posted 3 years ago #

    are the people that you say are 'authors' actually set to be 'Author' in their user role?

    by this i mean.. under your users section of the dashboard.. click 'edit' on any of the users you want to show and check that their role is set to Author..

    if you want a list of people from a different role let me know and ill make the changes

  15. sarahebkaiser
    Member
    Posted 3 years ago #

    Aha...what I really want to show is anybody who has written a post...

  16. sarahebkaiser
    Member
    Posted 3 years ago #

    So I'm thinking, I could access all post_authors != 0 from the posts table, and display those names in a list...and then I'd have to compare the user ID to the user table to get the user_url.

    Or, I could only list those users with the user type of "contributor" AND "admin"...that's the easy route but could cause problems when a contributor hasn't yet written a post.

    I want to add in links to each of the author pages but I think I can figure out how to do that.

    Again - I really appreciate your help!

  17. haxxxton
    Member
    Posted 3 years ago #

    oh wow haha ok this is a slightly different thing.. but not undoable..

    as for linking to their profile page.. i might have to leave that up to you.. you would just need to add the link where the # is currently

    <?php
    $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts" WHERE post_type='page' AND post_status='publish'); ?>
    $authors = array_unique($authors);
    
    <ul>
    <?php
    for($j=0; $j < count($authors); $j++){
          echo "<li><a href=\"#\" title=\"".$authors[$j]."'s profile\">".$authors[$j]."</a></li>";
      }
    ?>
    </ul>

    ive included the links and title now
    this should give you a list of anyone that has ever made a post

  18. sarahebkaiser
    Member
    Posted 3 years ago #

  19. sarahebkaiser
    Member
    Posted 3 years ago #

    By the way, you are the most patient/helpful person I've ever had the pleasure of meeting on the internet. Seriously, I'm bowled over by how helpful you've been.

  20. haxxxton
    Member
    Posted 3 years ago #

    wow im really not with it tonight.. im putting errors all over the place and just not thinking..

    im helpful cos im procrastinating from working on a project of my own that is being a handful haha but thank you none-the-less

    <?php
    
        $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts WHERE post_type='page' AND post_status='publish'");
    	$authors = array_unique($authors);
    	?>
    
        <ul>
        <?php
        for($j=0; $j < count($authors); $j++){
              $username = get_userdata($authors[$j]);
              echo "<li><a href=\"#\" title=\"".$username->user_login."'s profile\">".$username->user_login."</a></li>";
          }
        ?>
        </ul>

    there is the code for pulling all users that have ever posted a post and displaying a list of their names that link currently to #

  21. sarahebkaiser
    Member
    Posted 3 years ago #

    Well, everything you've done makes sense and should work! Thank you times a million.

  22. haxxxton
    Member
    Posted 3 years ago #

    yay i can see that its working :)

    in so far as linking to the users profile.. it looks like currently you only have 3 profile pages.. you might also want to give their php files more specific names than 'richard.php' what would truly make life easier for yourself would be to user php page names that are basically the same as their usernames..

    because you cant use "."s in page names you could try a

    str_replace(".","-",$username->user_login);

    to swap and .s to -s in the link section..

    so the code would become

    <?php
    
        $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts WHERE post_type='page' AND post_status='publish'");
    	$authors = array_unique($authors);
    	?>
    
        <ul>
        <?php
        for($j=0; $j < count($authors); $j++){
              $username = get_userdata($authors[$j]);
              echo "<li><a href=\"";
              bloginfo('url');
              echo"/authors/".str_replace(".","-",$username->user_login).".php\" title=\"".$username->user_login."'s profile\">".$username->user_login."</a></li>";
          }
        ?>
        </ul>

    this would take admin and link it to
    http://www.allthingsplc.info/authors/admin.php

    and richard.dufour and link it to
    http://www.allthingsplc.info/authors/richard-dufour.php

  23. sarahebkaiser
    Member
    Posted 3 years ago #

    New issue! This isn't showing ALL the authors who have written posts. Which is weird, because your query looks perfectly written.

    For example, post_author=1059 (Bill Ferriter) wrote this:
    http://www.allthingsplc.info/wordpress/?p=701

    His info should be pulled...that is a post (I changed "page" to "post" because it makes more sense for my situation), it's status set to publish...

    It seems like for some reason this database query isn't looking at all the posts, maybe only the first 30? Why would that happen?

  24. sarahebkaiser
    Member
    Posted 3 years ago #

    As for author pages, I'm using the automatically generated ones, like this:

    http://www.allthingsplc.info/wordpress/?author=1059

    so I'll just pull the post_author number to get that link once I get the issue of all the people who have written posts showing up figured out:)

  25. haxxxton
    Member
    Posted 3 years ago #

    it also occurs to me that you might not want show profiles like 'admin'

    i would suggest creating an array of userID's you DONT want in your list.. here i have included '1' which is the default userID for the admin account..

    <?php
    
        $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts WHERE post_type='page' AND post_status='publish'");
    	$authors = array_unique($authors);
            $dont_show = array("1"); //add more users here in ""s comma seperated
    	?>
    
        <ul>
        <?php
        for($j=0; $j < count($authors); $j++){
              if(!in_array($dont_show,$authors[$j]){
              $username = get_userdata($authors[$j]);
              echo "<li><a href=\"";
              bloginfo('url');
              echo"/authors/".str_replace(".","-",$username->user_login).".php\" title=\"".$username->user_login."'s profile\">".$username->user_login."</a></li>";
              }
          }
        ?>
        </ul>

    hope that helps

    also when you're happy with it how you want.. dont forget to change this thread's status to resolved in the dropdown box in the right sidebar :)

    ----------->>>>>>>>>>

    enjoy :)

  26. haxxxton
    Member
    Posted 3 years ago #

    <?php
    
        $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts WHERE post_type='post' AND post_status='publish'");
    	$authors = array_unique($authors, SORT_NUMERIC);
            $dont_show = array("1"); //add more users here in ""s comma seperated
    	?>
    
        <ul>
        <?php
        for($j=0; $j < count($authors); $j++){
              if(!in_array($dont_show,$authors[$j]){
              $username = get_userdata($authors[$j]);
              echo "<li><a href=\"";
              bloginfo('url');
              echo"/?author=".$authors[$j]."\" title=\"See all ".$username->user_login."'s posts\">".$username->user_login."</a></li>";
              }
          }
        ?>
        </ul>

    that should be what you are looking for...

    im not sure why it wouldnt tell you all of the users..

    array_unique looks for instances where two array items are IDENTICAL and removes them.. so "one" "one" will become "one".. but "One" "one" will stay the same.. and these id's are numbers.. so theres no way they should get confused :S

    EDIT: figured it out.. i think its cos removing duplicates leaves gaps in the array.. just need to re sort it.. code should be right now..

  27. sarahebkaiser
    Member
    Posted 3 years ago #

    I'm guessing haxxxton is asleep or away by now, but this code still isn't working for me.

    I'm now getting nothing echoed. I left out the exception for admin because admin actually is a post author in our case, one that we want to keep.

  28. sarahebkaiser
    Member
    Posted 3 years ago #

    Hey, got it working. I'll post the code for the final working version in case anyone's interested.

    http://www.allthingsplc.info/wordpress/archives-author.php

    <?php
    $authors = $wpdb->get_col("SELECT post_author FROM $wpdb->posts WHERE post_type='post' AND post_status='publish'");
    $authors = array_unique($authors);
    $authors = array_values($authors);
    ?>
    
    <ul class="authorarchives">
    <?php
    
    for($j=0; $j < count($authors); $j++){
    		$username = get_userdata($authors[$j]);
    		echo "<li><a href=\"";
    		bloginfo('url');
    		echo"/?author=".$authors[$j]."\" title=\"See all ".$username->user_login."'s posts\">".$username->display_name."</a></li>";  }
    ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic