solostream

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

| March 27, 2008 | 15 Comments

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.

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tags:

Category: Shorties

About Miriam Schwab: View author profile.

Elegant WP Themes

Comments (15)

Trackback URL | Comments RSS Feed

  1. Ryan says:

    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 :(

  2. Andrew says:

    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.
    }
    }

  3. Andrew says:

    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.

  4. Miriam Schwab says:

    @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!!

  5. [...] today Miriam at WordPress Garage posted a quick challenge: how to display a list of authors who had posted in a particular category. I had an idea at the time but I wasn’t in a position to test the idea, so now I am home I [...]

  6. Mark says:

    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. Mark says:

    Sorry,

    i have the arguments backwards for strstr:

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

  8. Mark says:

    argh! lol i must be tired

    instead of echo $a, use:

    the_author_posts_link ();

  9. Rebecca says:

    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. [...] Challenge: how to display only authors who have written in a specific category? | WordPress Garage < br/> automatically display on a category page only the authors who have written articles in that particular category on your WordPress Blog [...]

  11. Ales says:

    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;
    ?>

  12. dvbii says:

    I know this is a long time after this was posted, however, when googling the problem, I found this, so I thought i would share an update to what we have working..based on the last post, this code was placed in the sidebar.php and only shows on the category pages listing all authors with posts in the current category.

    <?php if (is_category()) {?>
    <?php
    $current_category = single_cat_title(“”, false);
    $author_array = array();
    $args = array(
    ‘numberposts’ => -1,
    ‘category_name’ => $current_category,
    ‘orderby’ => ‘author’,
    ‘order’ => ‘ASC’
    );
    $cat_posts = get_posts($args);
    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/”;
    echo $auth_link;
    echo “‘>”;
    echo $auth;
    echo “</a>”;
    echo “<br />”;
    endforeach;
    ?>
    <?php }?>

    • Tara says:

      this is great however does anyone know how to make a list of categories then under each category a sub-menu of users who have posted in that category

      for example:

      category 1
      -user with posts in category 1
      -user with posts in category 1
      category 2
      -user with posts in category 2
      -user with posts in category 2
      -user with posts in category 2
      etc.. 
       

  13. Tara says:

    Does anyone know how to make a list of categories with a sub menu of authors who have posted in each category so:
     
    category 1
    -author who has posted in category 1
    -author who has posted in category 1
     
    category 2
    -author who has posted in category 2
    -author who has posted in category 2
     
    ETC

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Shorties (5 of 33 articles)