Co-Authors Wordpress Plugin: coauthors_wp_list_authors function not working correctly

Posted by rayne on Stack Overflow See other posts from Stack Overflow or by rayne
Published on 2010-03-29T09:30:54Z Indexed on 2010/03/29 9:33 UTC
Read the original article Hit count: 554

Filed under:
|

The Co-Authors Plus Plugin for Wordpress has a very annoying bug. The custom function coauthors_wp_list_authors lists authors the same way the wordpress function wp_list_authors does, but it does not include authors in the list who don't have a post of their own - if they have only entries in which they are listed as co-author but not as author, they will not be included in the list. That is of course missing a very important point.

I've looked at the faulty SQL statement, but unfortunately my knowledge of advanced SQL, especially when it comes to JOINs, as well as my knowledge of the wp database structure is too limited and I remain clueless.

There is a topic in the WP support forum, but unfortunately the information there is very outdated and the fix is not applicable anymore. I couldn't find any other, more current solutions on the internet. I'd be glad if somewhere here could help fix the SQL statement so it also lists co-authors who don't have posts where they're the sole author, as well as display the correct post count for all authors.

Here's the entire function for reference purposes with a comment marking the SQL statement:

            function coauthors_wp_list_authors($args = '') {
                global $wpdb, $coauthors_plus;

                $defaults = array(
                    'optioncount' => false, 'exclude_admin' => true,
                    'show_fullname' => false, 'hide_empty' => true,
                    'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
                    'style' => 'list', 'html' => true
                );

                $r = wp_parse_args( $args, $defaults );
                extract($r, EXTR_SKIP);
                $return = '';

                $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");



                $author_count = array();

                # this is the SQL statement which doesn't work correctly: 
                $query  = "SELECT DISTINCT $wpdb->users.ID AS post_author, $wpdb->terms.name AS user_name, $wpdb->term_taxonomy.count AS count";
                $query .= " FROM $wpdb->posts";
                $query .= " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
                $query .= " INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
                $query .= " INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)";
                $query .= " INNER JOIN $wpdb->users ON ($wpdb->terms.name = $wpdb->users.user_login)";
                $query .= " WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' );
                $query .= " AND $wpdb->term_taxonomy.taxonomy = '$coauthors_plus->coauthor_taxonomy'";
                $query .= " GROUP BY post_author"; 

                foreach ((array) $wpdb->get_results($query) as $row) {
                    $author_count[$row->post_author] = $row->count;
                }

                foreach ( (array) $authors as $author ) {

                    $link = '';

                    $author = get_userdata( $author->ID );
                    $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
                    $name = $author->display_name;

                    if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
                        $name = "$author->first_name $author->last_name";

                    if( !$html ) {
                        if ( $posts == 0 ) {
                            if ( ! $hide_empty )
                                $return .= $name . ', ';
                        } else
                            $return .= $name . ', ';

                        continue;
                    }

                    if ( !($posts == 0 && $hide_empty) && 'list' == $style )
                        $return .= '<li>';
                    if ( $posts == 0 ) {
                        if ( ! $hide_empty )
                            $link = $name;
                    } else {
                        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s", 'co-authors-plus'), $author->display_name) ) . '">' . $name . '</a>';

                        if ( (! empty($feed_image)) || (! empty($feed)) ) {
                            $link .= ' ';
                            if (empty($feed_image))
                                $link .= '(';
                            $link .= '<a href="' . get_author_feed_link($author->ID) . '"';

                            if ( !empty($feed) ) {
                                $title = ' title="' . esc_attr($feed) . '"';
                                $alt = ' alt="' . esc_attr($feed) . '"';
                                $name = $feed;
                                $link .= $title;
                            }

                            $link .= '>';

                            if ( !empty($feed_image) )
                                $link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />';
                            else
                                $link .= $name;

                            $link .= '</a>';

                            if ( empty($feed_image) )
                                $link .= ')';
                        }

                        if ( $optioncount )
                            $link .= ' ('. $posts . ')';

                    }

                    if ( !($posts == 0 && $hide_empty) && 'list' == $style )
                        $return .= $link . '</li>';
                    else if ( ! $hide_empty )
                        $return .= $link . ', ';
                }

                $return = trim($return, ', ');

                if ( ! $echo )
                    return $return;
                echo $return;
            }

© Stack Overflow or respective owner

Related posts about wordpress-plugin

Related posts about sql