List all Authors with Posts in Category

Sourced here, this will help filter content posted by multiple users on a blog. See the original code below, which uses a dropdown to filter between the users / categories:

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

Here is the customized version of the code, calling the Post > Category, and linking to the Author page:

$category = get_queried_object();

$taxonomy_name = 'category'; //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' => 'post', //Change to your custom post type

'tax_query' => array(

        array(

            'taxonomy' => 'category', //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; ?>

<li><a href="<?php get_bloginfo(); ?>/author/<?php echo $nicename; ?>"><?php echo $auth; ?></a></li>

<?php //creates the URL to our term, filtered by author

endforeach;

 

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.