How do you query posts from more than one author in Wordpress?
something like:
query_posts('author=9,10&post_status=publish&orderb开发者_开发知识库y=date')
This is not working though.
According to this documentation there is no official support: http://codex.wordpress.org/Function_Reference/query_posts - under "Author Parameters" heading.
However, digging into the source code reveals otherwise: http://wordpress.taragana.net/nav.html?wp-includes/classes.php.html#query_posts.
$author_array = preg_split('/[,\s]+/', $q['author']);
for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
$whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
}
$whichauthor .= ')';
To see if your comma-separated list is being used correctly I would go ahead and start throwing debug lines into the WP_Query::get_posts()
function. For example, what is the final value of $whichauthor
?
I think this is the right way to do it :
function yclads_posts_where_followed_authors($where) {
global $wpdb;
$authors_ids = array(0,1,2,3);//array of authors IDs
if (count($authors_ids) > 0)
{
$where .= ' AND ' . $wpdb->posts . '.post_author IN(' . implode (',',$authors_ids) . ') ';
}
else
{
$where .= ' AND 1=2'; //do not return results
}
return $where;
}
add_filter('posts_where', 'posts_where_filter_authors');
The query_posts() function was only meant to support querying one author at a time. If you want to display multiple authors, I suggest trying one of the following:
- Create your own custom query to include multiple author IDs. You can use $wpdb->query() to run just about any SQL statement on the database, so you can definitely get a list of all posts by multiple authors, it just takes more work.
- The second option is to group your posts by author and run a separate WordPress loop for each author ID. This breaks your content up somewhat, but you could preface each section with a short author bio. There's example code on this site.
This slightly different loop will work mutliple times on a page or post with a different query parameters:
<?php $my_query = new WP_Query('author=9&post_status=publish&orderby=date'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php endwhile; ?>
精彩评论