WordPress.org

Ready to get started?Download WordPress

Forums

How to get all usernames and their posts? Kind of "user loop". (7 posts)

  1. hilj
    Member
    Posted 3 years ago #

    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 :)

  2. hilj
    Member
    Posted 3 years ago #

    Bump :) Any suggestions?

  3. maerk
    Member
    Posted 3 years ago #

    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.

  4. hilj
    Member
    Posted 3 years ago #

    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 :)

  5. maerk
    Member
    Posted 3 years ago #

    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 ;)

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    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.
    http://codex.wordpress.org/AJAX
    http://codex.wordpress.org/AJAX_in_Plugins

    NOTE:
    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.

  7. hilj
    Member
    Posted 3 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags