Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using wp_list_authors function to list all the users in my blog, however it doesn't display the users who wrote custom post types. it only displays the users who wrote regular posts. Is there a way of displaying all the users who wrote posts and also including the users who wrote custom post types in the same list?

I was looking for such answer and could not find it anywhere on the web. any help will be appreciated

Thanks, Gil

share|improve this question
    
wp_list_authors has the post type hardcoded into the query, but you could create your own function based on it and change the query to select IN( 'post', 'another_type' ). see the function in source. –  Milo Mar 12 '13 at 23:48
    
Do u mean - changing the core file? –  gil hamer Mar 12 '13 at 23:52
1  
no, create your own function with a unique name that replicates the core function, but change the query to query multiple post types. –  Milo Mar 12 '13 at 23:55

1 Answer 1

up vote 0 down vote accepted

Add the following function in functions.php and use custom_wp_list_authors function in place of wp_list_authors in your theme where you want to display authors who wrote custom post types post.

function custom_wp_list_authors($args = '') {
global $wpdb;

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

$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );

$return = '';

$query_args = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number' ) );
$query_args['fields'] = 'ids';
$authors = get_users( $query_args );

    $custom_post_types = get_post_types(array('_builtin' => false));
    if(!empty($custom_post_types)){
        $temp = implode ("','", $custom_post_types);
        $custom_post_types = "'"; 
        $custom_post_types .= $temp; 
        $custom_post_types .= "','post'";
    }else{
        $custom_post_types .= "'post'";
    }
$author_count = array();        
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type in ($custom_post_types)  AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
    $author_count[$row->post_author] = $row->count;

foreach ( $authors as $author_id ) {
    $author = get_userdata( $author_id );

    if ( $exclude_admin && 'admin' == $author->display_name )
        continue;

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

    if ( !$posts && $hide_empty )
        continue;

    $link = '';

    if ( $show_fullname && $author->first_name && $author->last_name )
        $name = "$author->first_name $author->last_name";
    else
        $name = $author->display_name;

    if ( !$html ) {
        $return .= $name . ', ';

        continue; // No need to go further to process HTML.
    }

    if ( 'list' == $style ) {
        $return .= '<li>';
    }

    $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $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 ) . '"';

        $alt = $title = '';
        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 . ')';

    $return .= $link;
    $return .= ( 'list' == $style ) ? '</li>' : ', ';
}

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

if ( !$echo )
    return $return;

echo $return;
}
share|improve this answer
    
Will this include also the authors who wrote regular posts in the same loop? –  gil hamer Mar 13 '13 at 7:30
    
yes it will include the authors who wrote regular posts. –  Vinod Dalvi Mar 13 '13 at 7:31
    
is this how you display it in the site: <?php custom_wp_list_authors; ?> I tried that and it's not working –  gil hamer Mar 13 '13 at 7:37
    
I think I got it: <?php custom_wp_list_authors(); ?>.. it is working now! Thanks @Vinod Dalvi –  gil hamer Mar 13 '13 at 7:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.