0

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
6
  • 1
    On second thought: Are you asking about authors of a category or authors who have posted in the category?
    – JCL1178
    Jan 13, 2013 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, 2013 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, 2013 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, 2013 at 10:52
  • @s_ha_dum Would you look at this picture? How can i do it?
    – Genxer
    Jan 13, 2013 at 13:45

2 Answers 2

3

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>';
    }
    ?>
1
  • This is an acceptable way if you also use other elements of the original post objects, such as the posts titles and descriptions. If you don't need these, it's overkill and will severely slow down the site depending on the amount of posts (or categories if you even do this several times on a page). In his answer to a duplicate question @PieterGoosen showed how to avoid this problem: wordpress.stackexchange.com/a/231588/148976
    – Playnary
    Aug 2, 2019 at 10:20
1

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.

1
  • Would you look at this picture? How can i do it?
    – Genxer
    Jan 13, 2013 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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