Here’s a simple way to achieve what you need with Taxonomies.
Supposed you have
- registered a taxonomy named Authors.
- Created a posted and assigned one author to that post -> Albert Einstein
3.You want only the total count of posts.
Now you want to retrieve all posts that have taxonomy = Authors and print each author name and the amount of posts with that author.
//Get all authors that have been assigned to a post
$authors = get_terms("authors");
//You can use print_r to see the values in the array
echo "<pre>";
//print_r($authors);
echo "</pre>";
//Loop through each author
foreach($authors as $author){
//Search posts with the author name
$query = new WP_Query(array(
"post_type"=>"post",
'tax_query' =>
array(
array(
'taxonomy' => 'authors',
'field' => 'term_id',
'terms' => $author->term_id
)
)
));
echo "<h2>Found " . $query->found_posts . " posts with author = " . $author->name . "</h2>";
}
You can format the output however you want. WordPress automatically creates the link structure (depending how your permalinks are setup). So you just need to create templates for the links wordpress created.
UPDATE:
I am assuming that when you go to /author/einstein-albert that you can already see all posts with the term einstein-albert.
If so, the you just need to form the link using the object you have in the foreach loop:
Something similar to this should be in the foreach loop:
echo "<a href="https://wordpress.stackexchange.com/" . $author->taxonomy . "https://wordpress.stackexchange.com/" . $author->slug . "">$author->name ($query->found_posts)</a>";
That should echo <a href="http://wordpress.stackexchange.com/author/einstein-albert">Albert Einstein(2)</a>