Challenge: how to display only authors who have written in a specific category?
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.
Category: Shorties










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
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.
}
}
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.
@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!!
[...] 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 [...]
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;
}
Sorry,
i have the arguments backwards for strstr:
if(!strstr($authors,$a))
{
echo $a;
$authors.=$a;
}
argh! lol i must be tired
instead of echo $a, use:
the_author_posts_link ();
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
I have the same problem here http://wordpress.org/support/topic/169195
[...] 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 [...]
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;
?>