Support » Fixing WordPress » How to get all usernames and their posts? Kind of "user loop".

  • Hello,

    I have a multiauthor blog, or more like a database where users can add posts. I would need to display all users and the posts they have written.

    I was thinking displaying it as an accordion, by clicking the + it would expand showing all the posts the author has (see below).

    + Firstname Lastname
        •Post #1
        •Post #2
    + Fistname Lastname
        •Post #1
        •Post #2

    Very similar logic as normal wp loop has.

    All help is more than appreciated 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Bump 🙂 Any suggestions?

    How familiar are you with code?

    Without going into too much detail, you’d need to select all users and loop through them, then within each iteration, select the posts. Something like:

    users = SELECT user_id, first_name, last_name FROM wp_users;
    foreach (users as user) {
    // output user's name
    SELECT post_id, post_title FROM wp_posts WHERE user_id = user[user_id] AND post_status = published LIMIT 5;
    // output post

    That’s not proper PHP but it’s an outline of the code you’d need.

    Not that familiar. I tried to wrap my head around but I’m afraid that I’ll need little more assistance on this. Thanks so far 🙂

    Have you edited any code in the templates before? I’ll be able to help you with coding the template, but if you’re used to working with plugins and widgets I’m probably not the best person to help 😉

    If you have a large userbase, that ever increasing amount of queries is going to become expensive and use up alot of server resources, and likely in turn, bandwidth to, are you certain this code is going to be suitable for this particular site? Is it a low traffic site?

    You could negate the expense of these queries using an accordian(like you said in opening) that fetched a given users posts via ajax when expanded, but that’s not something that will happen without first acquainting yourself with creating and using Ajax in WordPress.

    If using Ajax is an option for you, you may want to start with a read of the following pages.

    There are also various blogs that deal with using Ajax in WordPress, but they tend to address different use cases, so i’ll leave you to google for the appropriate guides/examples.

    I know my way around wp templates, this issue has very little documentation.

    It’s a low traffic site and yeah I was thinking of some AJAX solution to the accordion. Thanks 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to get all usernames and their posts? Kind of "user loop".’ is closed to new replies.