繁体   English   中英

您如何在Wordpress中查询来自多个作者的帖子?

[英]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()函数仅旨在支持一次查询一位作者。 如果要显示多个作者,建议尝试以下操作之一:

  1. 创建自己的自定义查询以包含多个作者ID。 您可以使用$ wpdb-> query()在数据库上几乎运行任何SQL语句,因此,您肯定可以获得多位作者的所有帖子的列表,这需要更多的工作。
  2. 第二个选项是按作者对您的帖子进行分组,并为每个作者ID运行一个单独的WordPress循环。 这会使您的内容有些混乱,但是您可以在每个部分的开头加上简短的作者简介。 该站点上有示例代码。

这个略有不同的循环将使用不同的查询参数在页面或帖子上工作多个时间:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM