I have links going to category pages on a WordPress blog. These category pages show all the posts that are in that category and sub-categories. I want to group these lists by the authors.
I guess need to modify category.php to first list all the authors which have posts in that particular category and then run WP_Query multiple times for each author. The second part is okay, but I can’t figure out how to get the list of authors of only those posts in this category.
I don’t think you need to get the list of authors in a given category or use a bunch of individual
WP_Query
‘s, you just need to order posts by author and check to see when the author changes.Two parts to this. One to modify the loop on category pages to order by author. You can hook into
pre_get_posts
to do this.Now to get a list of authors.
Since
WP_Query
fetches all posts at once — it doesn’t stream data in — you canarray_map
the posts to get the Author IDs out and create your list. Because you altered the order of post above, the authors should come out in order. Wrapping this up in a function might be a good idea:Then you can use something like this in your
category.php
template to generate the list.Grouping the post by authors should just be a matter of compare the previous posts author with the current posts author.
Example:
make your query to order by author
example for WP_Query
or if you use query_posts