• Trying to create an authors page that is sorted alphabetically. Returning all the right stuff, just can’t get ORDER BY or any sort to work.

    <?php
    $querystr = "
    SELECT DISTINCT post_author
    FROM $wpdb->posts
    WHERE post_author!= '1'
    ";
    
    $authors = $wpdb->get_results($querystr, OBJECT);
    
    if($authors):
    foreach($authors as $author):
    ?>

    Any help would be appreciated. I am new to wordpress/php. I am also using SQL not MySQL.

Viewing 6 replies - 1 through 6 (of 6 total)
  • SQL is the language, MySQL is the specific SQL database. Unless you’re running on a VPS or dedicated server where you get to do/install whatever you want, you’re probably using MySQL.

    To list all your authors, it’s probably easier to use wp_list_authors unless you’re looking to do something beyond that.

    Function Reference/wp list authors

    Thread Starter dabrisz

    (@dabrisz)

    I’m on a dedicated iis box required by my it. I’m creating authors pg which displays the nickname, description, website, etc. from the user profiles. All that works fine. I just can’t figure out how to order by or sort the author A to Z. Currently ordered by post_ author or ID, desc.

    by default, MySQL should order alphabetically if you throw in ORDER BY ‘whatever the column is that has the author name’ in your SQL Query.

    It looks like the post_author column is an INT data type, so I’m guessing that it’s the primary key and just an ID# for each particular author. You’ll need to ORDER BY whatever column has the name of the author and you should be fine

    Now knowing want you’re looking to do, I would think using get_users or the WP_User_Query class would be easier.

    Function Reference/get users
    Class Reference/WP User Query

    Something like this perhaps:

    $args = array(
        'exclude' => array( 1 ),
        'who' => 'authors',
        'orderby' => 'display_name'
    );
    
    $authors = get_users( $args );

    But if you really want to use a custom query, you’ll need to do as wspencer said and use ORDER BY. Since user names/display names aren’t kept in the posts table, you’ll have to find a way to use the users table. Might be as simple as ORDER BY $wpdb->posts.display_name, or you might have to look into joins. You could also run a separate query on the users table then only display users that have ID’s in both results. I’m not much of an SQL expert. ??

    And just an aside, if you’re only grabbing data from a single column, you can also take a look at $wpdb->get_col:

    https://codex.www.ads-software.com/Class_Reference/wpdb#SELECT_a_Column

    Thread Starter dabrisz

    (@dabrisz)

    Thanks for the suggestions. I will try these and post my results. Thanks again!

    Thread Starter dabrisz

    (@dabrisz)

    Still working on this, but it appears a better sql query is the answer.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Author's Page’ is closed to new replies.