How do you request posts from multiple authors in Wordpress?

How do you request posts from multiple authors in Wordpress?

sort of: query_posts('author=9,10&post_status=publish&orderby=date')

This does not work.

+2


a source to share


4 answers


According to this documentation, there is no official support: http://codex.wordpress.org/Function_Reference/query_posts - under the heading "Author Options".

However, digging through the source code shows 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 the comma separated list is being used correctly, I would go and start throwing debug lines into the function WP_Query::get_posts()

. For example, what is the final value $whichauthor

?

+2


a source


I think this is the correct 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');

      

+2


a source


The query_posts () function is only intended to support a query for one author at a time. If you want to display multiple authors, I suggest trying one of the following:

  • Create your own request to include multiple author IDs. You can use $ wpdb-> query () to run almost 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 posts by author and run a separate WordPress loop for each author ID. This loosens your content a bit, but you can preface each section with a short biography of the author. Here is some sample code for this site .
0


a source


This slightly different loop will run multiple times per page or post with 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; ?>

      

0


a source







All Articles