I have this coding challenge that's beating my head
I built a site for an academic magazine, where some of the articles are written by two or more authors. This was easily acheived using either Co-Author or Co-Author Plus plugin, as shown in the following image.
Then, using this code, I built an authors archive where I display all wordpress authors with posts published.
<?php
$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts.' JOIN wp_usermeta ON user_id = post_author WHERE meta_key=\'last_name\' ORDER BY meta_value');
if($authors):
foreach($authors as $author):
?>
<div class="author" id="author-<?php the_author_meta('user_login', $author->post_author); ?>">
<?php if(get_the_author_meta('description', $author->post_author)): ?>
<div class="description">
<h3><a href="<?php bloginfo('url'); ?>/author/<?php the_author_meta('user_nicename', $author->post_author); ?>"><?php the_author_meta('last_name', $author->post_author); ?>, <?php the_author_meta('first_name', $author->post_author); ?></a></h3>
<p><?php the_author_meta('description', $author->post_author); ?></p>
</div>
<?php else : ?>
<!-- <div class="description">
<p>No description in the author's profile.</p>
</div> -->
<?php endif; ?>
</div>
<?php endforeach; endif; ?>
But the code only displays "first" authors. i.e the first author assigned to a post, and not the second nor third. So, if the second or third author hasn't published another different post by himself, he won't appear on the authors list.
So, the question is, how can I modify this code to show all authors associated with a post? Maybe a string where author_has_post >= 0, and then how to prevent the admin author from appearing on the list
I have this coding challenge that's beating my head
I built a site for an academic magazine, where some of the articles are written by two or more authors. This was easily acheived using either Co-Author or Co-Author Plus plugin, as shown in the following image.
Then, using this code, I built an authors archive where I display all wordpress authors with posts published.
<?php
$authors = $wpdb->get_results('SELECT DISTINCT post_author FROM '.$wpdb->posts.' JOIN wp_usermeta ON user_id = post_author WHERE meta_key=\'last_name\' ORDER BY meta_value');
if($authors):
foreach($authors as $author):
?>
<div class="author" id="author-<?php the_author_meta('user_login', $author->post_author); ?>">
<?php if(get_the_author_meta('description', $author->post_author)): ?>
<div class="description">
<h3><a href="<?php bloginfo('url'); ?>/author/<?php the_author_meta('user_nicename', $author->post_author); ?>"><?php the_author_meta('last_name', $author->post_author); ?>, <?php the_author_meta('first_name', $author->post_author); ?></a></h3>
<p><?php the_author_meta('description', $author->post_author); ?></p>
</div>
<?php else : ?>
<!-- <div class="description">
<p>No description in the author's profile.</p>
</div> -->
<?php endif; ?>
</div>
<?php endforeach; endif; ?>
But the code only displays "first" authors. i.e the first author assigned to a post, and not the second nor third. So, if the second or third author hasn't published another different post by himself, he won't appear on the authors list.
So, the question is, how can I modify this code to show all authors associated with a post? Maybe a string where author_has_post >= 0, and then how to prevent the admin author from appearing on the list
Share Improve this question asked Dec 24, 2010 at 1:26 Sergio MajlufSergio Majluf 3223 silver badges9 bronze badges2 Answers
Reset to default 2This would make sense as an inclusion for the Co-Authors Plus plugin but you can't always depend on plugin developers to anticipate all our needs. Sometimes we just have to build for ourselves! :-)
(source: mikeschinkel)
Co-Authors Stored as Terms of the Taxonomy 'author'
Turns out what you want to do is not too hard. I'll praise the plugin developers for using what I consider best practices; they created a taxonomy called 'author'
and for the names of the terms they used the author's user_login
field.
Use get_terms()
to get a list of Co-Authors
So it's easy to get a list of authors just by calling the WordPress core function get_terms()
passing it the taxonomy 'author'
and specifying that you only need the term 'name'
field.
Direct SQL required for a List of Author IDs
Unfortunately thumbs down to WordPress core for providing no way to get a list of author IDs based of a list of user_login
values other than with direct SQL.
Our get_coauthor_list()
function
Taken together we can create our simple get_coauthor_list()
function to return a list of author IDs for all the available Co-Authors. Here's the code (you can add this to the bottom of your theme template file, or better put in your theme's functions.php
file, or even better still get the Co-Authors Plus folks to add to their plugin):
function get_coauthor_list() {
global $wpdb;
$authors = implode("','",get_terms('author',array('fields'=>'names')));
$sql = "SELECT ID " .
"FROM {$wpdb->users} " .
"WHERE user_login IN ('{$authors}') " .
"ORDER BY display_name";
return $wpdb->get_col($sql);
}
Using get_coauthor_list()
in your Theme
From there it's pretty simple to just add to your theme template file. I've modified your code slightly to use the get_coauthor_list()
function and to follow some best practices such as using the get_author_posts_url()
function found in WordPress core:
<h2>Archives by Author:</h2>
<?php
$authors = get_coauthor_list();
if($authors)
foreach($authors as $author_id):
?>
<div class="author" id="author-<?php the_author_meta('user_login', $author_id); ?>">
<h3>
<a href="<?php echo get_author_posts_url($author_id); ?>">
<?php the_author_meta('display_name', $author_id); ?>
</a>
</h3>
<div class="description">
<p>
<?php if($author_description = get_the_author_meta('description',$author_id)): ?>
<?php echo $author_description; ?>
<?php else : ?>
No description in the author's profile.
<?php endif; ?>
</p>
</div>
</div>
<?php
endforeach;
?>
And when it's all said and done it looks like this:
(source: mikeschinkel)
P.S. Ironically to figure this out for you I had to install the plugin which then required me to debug (what turned out to be an insignificant) error message on plugin activation that resulted in my submitting this bug report; hopefully they will correct the bug in future releases.
Wordpress by design knows only one author per post and displays it like so in listings like the archives.
So you need to add additional, individual code depending on the plugins data. In the code example you posted you should need to take care that the SQL query you formulate returns more then only one row. Obviously the one you're currently using is returning only the author saved by wordpress. Which is as I already wrote, a single one by design.
You should discuss this issue with the plugin author(s), as it is specific to the plugin. So they know into which problems you ran while using the plugin(s). Probably they have a forum as well where you can get code that is already ready to use.