Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    It could be streamlined a bit, but essentially, paginating a list of variable length items is not that simple. If you were to look at posts_nav_link() source, it doesn’t look too bad because of sub-functions. By the time you drill down through all the sub-function levels you find it amounts to a lot of code. There’s a lot going behind many convenient template tags.

    Thread Starter TIEro

    (@tiero)

    It could be streamlined a LOT, like the “paged” stuff in post navigation!

    Worked around it by building the SQL query myself through php and paginating it with LIMIT and OFFSET – simpler, more flexible for filtering and sorting, and it’s even quicker. 🙂

    Thanks for taking the time to answer!

    Moderator bcworkz

    (@bcworkz)

    You’re going to make claims like that and not provide an example of your solution??? 😉

    Truth be told, I didn’t look that close at the SE example, I sorta assumed it was using LIMIT and OFFSET.

    If you’re not comfortable sharing, that’s fine, but it could help someone landing here through search results.

    Thread Starter TIEro

    (@tiero)

    It’s kinda specific, to be honest, but I’m happy to share. My issue wasn’t so much with the two queries (I can see why that’s necessary, since you need a count for the LIMIT and OFFSET) as with the complexity of handling the page links with WP’s built-in function (which is frankly horrible, presumably for flexibility’s sake).

    Bear in mind that this solution takes parameters on a user list page:

    $show = user role to show (they only ever have one in this system)
    $sort_by = field to sort by (one of which is a user_meta, hence table c)
    $sort_order = ASC or DESC
    $num_posts = items per page
    $user = specific user ID from a dropdown
    $paged = current page number

    I also wanted the sorted/filtered URLs to be bookmarkable (if that’s a word) so that I could come back to them easily. I do the same on every sorted/filtered list in the front end, so it’s easier for visitors to set things up how they want, then bookmark it.

    My coding skills are not that good, but this works a treat for me:

    http://pastebin.com/dd4Zd49V

    Feel free to tell me how awful it is. 🙂

    Moderator bcworkz

    (@bcworkz)

    Aww, give yourself some credit! It’s organized, readable, and it works. Anything else is not that important IMO. Well done. Paginating custom queries is an issue many struggle with. While your example is necessarily specific to your situation, it wouldn’t take much to adapt it to a different circumstance. Thanks for sharing!

    Thread Starter TIEro

    (@tiero)

    Two additional notes:

    1. You don’t need the $sort_clause on the first query – it just counts, so save the sorting time.

    2. I use $num_posts=’-1′ for ‘all posts’. If you do the same, you’ll need to adjust the $limit_clause line:

    $limit_clause = ($num_posts == ‘-1’) ? “” : “LIMIT ” . $num_posts . ” OFFSET ” . (($bottom_limit>0) ? $bottom_limit : 0);

    Just spotted (2) because my ‘all’ lists were empty! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How do I paginate a custom user list?’ is closed to new replies.