WordPress.org

Make WordPress Core

Ticket #10329: author-template.diff

File author-template.diff, 4.2 KB (added by takaitra, 3 years ago)

Patch to add sort_by and count_limit options.

  • author-template.php

     
    245245 * or as a string.</li> 
    246246 * <li>html (bool) (true): Whether to list the items in html for or plaintext. 
    247247 * </li> 
     248 * <li>sort_by (string) ('name'): The sort order of the list. Can be 
     249 * alphabetical ('name') or by number of posts ('post_count').</li> 
     250 * <li>count_limit (int) (-1): When passed a positive integer, limit the list 
     251 * to this number of authors.</li> 
    248252 * </ul> 
    249253 * 
    250254 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors 
     
    259263                'optioncount' => false, 'exclude_admin' => true, 
    260264                'show_fullname' => false, 'hide_empty' => true, 
    261265                'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 
    262                 'style' => 'list', 'html' => true 
     266                'style' => 'list', 'html' => true, 'sort_by' => 'name', 
     267                'count_limit' => -1 
    263268        ); 
    264269 
    265270        $r = wp_parse_args( $args, $defaults ); 
    266271        extract($r, EXTR_SKIP); 
    267272        $return = ''; 
    268273 
    269         /** @todo Move select to get_authors(). */ 
    270         $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name"); 
     274        if ( $show_fullname ) { 
     275                $author_name_field = "CASE 
     276                WHEN TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) <> '' 
     277                THEN 
     278                        TRIM(CONCAT_WS(' ', COALESCE(meta_first_name.meta_value, ''), COALESCE(meta_last_name.meta_value, ''))) 
     279                ELSE 
     280                        $wpdb->users.display_name 
     281                END"; 
     282                $author_name_join = " 
     283                LEFT JOIN $wpdb->usermeta as meta_first_name ON meta_first_name.user_id = $wpdb->users.ID AND meta_first_name.meta_key = 'first_name' 
     284                LEFT JOIN $wpdb->usermeta as meta_last_name ON meta_last_name.user_id = $wpdb->users.ID AND meta_last_name.meta_key = 'last_name' 
     285                "; 
     286        } else { 
     287                $author_name_field = "$wpdb->users.display_name"; 
     288                $author_name_join = ""; 
     289        } 
    271290 
    272         $author_count = array(); 
    273         foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) { 
    274                 $author_count[$row->post_author] = $row->count; 
     291        if ( $sort_by == 'name' ) { 
     292                $author_sort = 'ORDER BY author_name'; 
     293        } else { 
     294                $author_sort = 'ORDER BY author_count DESC'; 
    275295        } 
    276296 
     297        $count_limit = intval( $count_limit ); 
     298        if ( $count_limit > 0 ) { 
     299                $author_limit = "LIMIT $count_limit"; 
     300        } else { 
     301                $author_limit = ''; 
     302        } 
     303 
     304        $authors = $wpdb->get_results(" 
     305                SELECT $wpdb->users.ID, $wpdb->users.user_nicename, COUNT($wpdb->posts.ID) as author_count, $author_name_field as author_name 
     306                FROM $wpdb->users 
     307                " . ( !$hide_empty ? "LEFT " : '' ) . "JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID 
     308                $author_name_join 
     309                WHERE ($wpdb->posts.post_type = 'post' OR $wpdb->posts.post_type IS NULL) 
     310                AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private' OR $wpdb->posts.post_status IS NULL) " . 
     311                ( $exclude_admin ? "AND $wpdb->users.user_login <> 'admin' " : '' ) . " 
     312                GROUP BY $wpdb->users.ID 
     313                $author_sort 
     314                $author_limit 
     315                "); 
     316 
    277317        foreach ( (array) $authors as $author ) { 
    278318 
    279319                $link = ''; 
    280320 
    281                 $author = get_userdata( $author->ID ); 
    282                 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0; 
    283                 $name = $author->display_name; 
     321                $posts = $author->author_count; 
     322                $name = $author->author_name; 
    284323 
    285                 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) 
    286                         $name = "$author->first_name $author->last_name"; 
    287  
    288324                if( !$html ) { 
    289325                        if ( $posts == 0 ) { 
    290326                                if ( ! $hide_empty ) 
     
    302338                        if ( ! $hide_empty ) 
    303339                                $link = $name; 
    304340                } else { 
    305                         $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>'; 
     341                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->author_name) ) . '">' . $name . '</a>'; 
    306342 
    307343                        if ( (! empty($feed_image)) || (! empty($feed)) ) { 
    308344                                $link .= ' ';