![](/img/trans.png)
[英]How do I query wordpress posts based on comment count and less than one year old?
[英]How do you query posts from more than one author in Wordpress?
您如何在Wordpress中查询来自多个作者的帖子?
类似于: query_posts('author=9,10&post_status=publish&orderby=date')
虽然这不起作用。
根据此文档,没有官方支持: http : //codex.wordpress.org/Function_Reference/query_posts-在“作者参数”标题下。
但是,深入研究源代码会发现其他情况: 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 .= ')';
要查看您使用逗号分隔的列表是否被正确使用,我将继续并开始将调试行添加到WP_Query::get_posts()
函数中。 例如, $whichauthor
的最终值是$whichauthor
?
我认为这是正确的方法:
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');
query_posts()函数仅旨在支持一次查询一位作者。 如果要显示多个作者,建议尝试以下操作之一:
这个略有不同的循环将使用不同的查询参数在页面或帖子上工作多个时间:
<?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; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.