So far as I know there is no ‘last posted on’ value saved for a user. You can find that information by referencing the post_date
in the *_posts
table against the author data in *_users
and *_usermeta
. With that in mind I can think of several solutions.
First Solution:
- Pull your authors. You need this later.
- Pull all of your published posts.
- Loop through the posts sorting things by author and keeping track of the most recent
post_date
for each author. - You can then loop through the sorted posts and print them out.
Here are the problems:
- You are pulling all of your published posts. This could potentially be a very heavy, slow page, and could even time out or run out of memory. The upside is that your existing code pulls all of your published posts as well but does so with a lot more database queries, which is usually but not always worse than one big query.
- You have to loop through all of your posts in order to sort them and then loop through again to print them. Again, heavy, slow, and may have execution timeout or memory problems. Again, you are more or less forced into this if you want to do what you are trying to do.
Second Solution:
Another solution would be to run a query to pull the latest post for each user ID, use that to sort the author data, then use your existing code to print them. I believe you will need to write the SQL for that query. I don’t think there is anything built in to do it. It may not have as many memory problems as the first solution but it is adding a query to the several queries you already have.
Third solution:
Still another solution would be to be to loop through your posts and add a ‘last posted’ value to *_usermeta
for each author. You’d have to update this whenever a post date is changed or a post published. You could then use the meta parameters of get_users
to sort on that ‘last posted’ field. This is more efficient than the immediately preceding solution but is more complicated. It still requires several trips to the database.
As you can imagine, there is a fair chunk of code, and debugging time, to any of these solutions. Apologies for not providing it. I don’t think I could get it right without actually implementing the several solutions and debugging them.
Maybe an alternative:
Another thing you might consider is doing your server, and your visitors, a favor by not printing posts for all authors on the same page, since that really works out to printing all published posts on the same page plus sorting overhead. For example, print an author ‘index’ page sorted by last post date if you want, with a link to an author detail page that has the posts on it (which is fact is very similar to what happens by default– authors show up at “/author/author-name”).
Also, you shouldn’t be using query_posts
. It clobbers the main query. Use get_posts
instead.