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.

How can I list the authors of posts in a category in WordPress?

On my blog have three authors and I want to list who written the posts in a category. For example:

CAT NAME: INTERNET     AUTHOR: JOHN, DOE, ALEX
CAT NAME: TECH         AUTHOR: JOHN
CAT NAME: CODE         AUTHOR: ALEX
share|improve this question
1  
On second thought: Are you asking about authors of a category or authors who have posted in the category? –  JCL1178 Jan 13 '13 at 3:44
    
Hi Can, welcome to WPSE! You don't need to specify WordPress in the title nor the content, we are here to deal exclusively with WP ;) :::: Maybe you can add custom fields to the category: in-house solution or plugin. –  brasofilo Jan 13 '13 at 4:53
    
I think this is a poorly written question. You want to be able to list the authors who have written posts in particular categories, correct? –  s_ha_dum Jan 13 '13 at 5:03
    
Exactly, i think, i was poorly write my question. I want to list who written post in a category. –  Genxer Jan 13 '13 at 10:52
    
@s_ha_dum Would you look at this picture? How can i do it? –  Genxer Jan 13 '13 at 13:45
show 2 more comments

2 Answers

WordPress Categories don't have authors in the traditional sense of the term. You should read up on the WordPress Taxonomies and also look through your wp_terms, wp_term_taxonomy and wp_term_relationships tables to see how WordPress organizes and uses that data.

share|improve this answer
    
Would you look at this picture? How can i do it? –  Genxer Jan 13 '13 at 13:42
add comment
up vote 1 down vote accepted

I found a solution.

CODE:

  <?php
    $cat_arr = get_categories(); // Get the list of Categories
    foreach ($cat_arr as $cat_obj) {
        $term_id = $cat_obj->term_id;

        // Print the Name
        ?>
        <br>
        CAT NAME: <?php echo $cat_obj->name ?>, AUTHOR: 
        <?php
        // Get all Posts of that Category
        $posts = get_posts(array('category'=>$term_id));

        $authors_arr = array();
        foreach ($posts as $post_obj) {
            $author_id = $post_obj->post_author;

                // In depends on where you put this code, the include of the file is required
            if (!function_exists('get_userdata')) {
                include '<your WP folder>/wp-includes/pluggable.php';
            }

            $user_obj = get_userdata($author_id);
                // Only Add the author is isn't already added, to avoid printed twice
            if (!in_array($user_obj->user_login, $authors_arr)) {
                $authors_arr[] = $user_obj->user_login; // Instead of user_login you can use any Database field of the "Users" table
            }
        }
        echo implode(', ', $authors_arr) . '<br>';
    }
    ?>
share|improve this answer
add comment

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.