Challenge: how to display only authors who have written in a specific category?

March 27, 2008 – 12:09 pm | by Miriam Schwab

Hi all – I was wondering if one of you can help us find a solution to a feature we would like to add to a WordPress site:

The site in question has multiple contributors. Most are dedicated to certain categories. We would like to automatically display on a category page only the authors who have written articles in that particular category.

For example, let’s say we have a category called “Cucumbers” and a category called “Tomatoes.” When someone clicks on the Cucumbers category we would like only the authors who have written posts categorized as Cucumber posts to appear, and on the Tomatoes category page, we would like only the authors who have written Tomato category posts to appear. There may be some overlap between the pages, since some authors may write posts for both the Cucumber and the Tomato categories.

One solution we thought of is to call the category we want and the number of posts from that category and then display the list of authors. The problem is it shows duplicate authors. Here’s the code:

<?php query_posts('showposts=20&cat=3'); ?>
<?php while (have_posts()) : the_post(); ?>

<?php the_author_posts_link (); ?>

<div style="clear:both"></div>
<?php endwhile;?>

Is there a way to prevent duplicate authors from appearing? Or is there a better way?

Thanks in advance for your help.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
  1. 12 Responses to “Challenge: how to display only authors who have written in a specific category?”

  2. By Ryan on Mar 27, 2008 | Reply

    I think you will need to make a program to grab the list of authors within that loop, remove any duplicates it finds and output them as HTML.

    I don’t have the foggiest idea how to implement that though :(

  3. By Andrew on Mar 27, 2008 | Reply

    Unfortunately I am not in a position to test any code at the moment so excuse the scruffyness and syntactic problems, but there are two ways I can think of.

    The first would be a direct database query, something along the lines of (I can’t recall the precise column names at the moment) SELECT DISTINCT post_author from wp_posts where term_id = x.

    If you want to avoid directly touching the database then one option is to use the get_posts function to return an array of posts for the category then loop through the posts finding the authors; e.g.:

    $author_array = array();
    $cat_posts = get_posts(“category=5″);

    foreach ($cat_posts as $cat_post){
    if ( !in_array($cat_post->post_author,$author_array)){ $author_array[] = $cat_post->post_author.
    }
    }

  4. By Andrew on Mar 27, 2008 | Reply

    Missed a bit. I am assuming that the code I have included above would return a user_id which can then be used with the get_userdata function to get the information necessary to create a link, as I say though I am not in a position to check this right now.

    I can check this and produce some working code this evening if no one has solved it by then. Feel free to drop me an e-mail.

  5. By Miriam Schwab on Mar 27, 2008 | Reply

    @Ryan – I also don’t have the foggiest. That’s why I’m asking here.

    @Andrew – what you wrote looks really promising. If no one else solves it by this evening, I’ll send you an email to see if you can help. I really appreciate it!!

  6. By Mark on Mar 28, 2008 | Reply

    This will do it for you:

    <?php while (have_posts()) : the_post();
    global $authordata;
    $a=get_author_posts_url ($authordata);
    if(!strstr($a,$authors))
    {
    echo $a;
    $authors.=$a;
    }

  7. By Mark on Mar 28, 2008 | Reply

    Sorry,

    i have the arguments backwards for strstr:

    if(!strstr($authors,$a))
    {
    echo $a;
    $authors.=$a;
    }

  8. By Mark on Mar 28, 2008 | Reply

    argh! lol i must be tired

    instead of echo $a, use:

    the_author_posts_link ();

  9. By Rebecca on Mar 30, 2008 | Reply

    Hi Mark – I tried implementing the code but I think it stopped displaying authors after the first author. any ideas? Did you get it to work for you on your own site? Thanks so much

  10. By Charly Silaban on Apr 24, 2008 | Reply

    I have the same problem here http://wordpress.org/support/topic/169195

  11. By Ales on Jun 23, 2009 | Reply

    This might be useful to someone.
    I’ve taken the code above and turned it into this…
    This works for me and does exactly what is described above…
    <?php
    $author_array = array();
    $cat_posts = get_posts(‘numberposts=-1&category=48&orderby=author&order=ASC’);

    foreach ($cat_posts as $cat_post) :
    if (!in_array($cat_post->post_author,$author_array)) {
    $author_array[] = $cat_post->post_author;
    }
    endforeach;

    foreach ($author_array as $author) :
    $auth = get_userdata($author)->display_name;
    $auth_link = get_userdata($author)->user_login;
    echo ‘<a href=”/author/’.$auth_link.’”>’;
    echo $auth;
    echo ‘</a>’;
    echo ‘<br />’;

    endforeach;
    ?>

  1. 2 Trackback(s)

  2. Mar 27, 2008: Display the authors that have posted to a particular category in WordPress
  3. Dec 1, 2008: Interesting websites for SEO, Web Marketing and everday work from Sante > > October 21st - November 28th

Post a Comment

Studio Press Premium Themes