While at first it may seem impossible, creating a dropdown list of all the authors with posts in a specific category is not actually all that difficult. This will work with custom taxonomies and custom post types as well, so it should suit the needs of just about anyone. Let’s get right to it!
The Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<form> <select onchange="window.open(this.options[this.selectedIndex].value,'_top')"> <option value="">Select by Author</option> <?php //Code to get a list of all authors with posts in this category, and then create a dropdown list of their names with links to their page $category = get_queried_object(); $taxonomy_name = 'custom_taxonomy_name_here'; //Change to reflect the name of your custom taxonomy $current_category = $category->slug; echo '<h1>' . $current_category . 'testing</h1>'; $author_array = array(); $args = array( 'posts_per_page' => -1, 'post_type' => 'custom_post_type_here', //Change to your custom post type 'tax_query' => array( array( 'taxonomy' => 'custom_taxonomy_name_here', //Change to reflect the name of your custom taxonomy 'field' => 'slug', 'terms' => $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; $nicename = get_userdata($author)->user_nicename; echo '<option value="' . get_term_link( $category->slug, $taxonomy_name ) . '/' . $nicename . '">' . $auth . '</option>'; //creates the URL to our term, filtered by author endforeach; ?> </select> </form> |
There are 3 spots you are going to need to fill in the name of your custom taxonomy or custom post type, all noted with comments. Make sure you change these to reflect what you are trying to do. Once you have that set up, you can drop this into a template file to make it display. keep in mind, this code works with an existing query to generate the list. In other words, it really only works on a category results page. I recommend creating a template file based on category.php and naming it taxonomy-custom_taxonomy_name.php, adding this code, and saving it to your template directory. Change “custom_taxonomy_name” to the name of your custom taxonomy.
The page that users will be brought to is actually an archive page for all posts with the given term (category), filtered by author. In this example, the link that is used uses pretty permalinks, so it may not work if you aren’t using pretty permalinks. There’s really no reason to not be using them, however, and there’s no downside to changing your permalink structure site-wide.
That should be it! Let me know if you have any trouble with the code in the comments, or you can reach me on Twitter @brianjonline or email me at brian@brianjohnsondesign.com.
One Comment on “Dropdown List of Authors with Posts in Category – WordPress”
How do I create a simple dropdown of authors with links. Basically, if I select a name from the dropdown, it takes me to their author page?
Here’s the code I’m using to create the dropdown:
‘authors’)); ?>
But this juts creates a dropdown, nothing links.
Thanks for any insight!!