Support » Developing with WordPress » Column Sorting with Pagination

  • Resolved metalandcoffee

    (@metalandcoffee)


    This is more so a call for advice and guidance about coding a desire functionality (without plugins since I’m trying to learn!).

    I created a custom template to query my custom post type and displaying it in a table format. The column headers are, for instance, First Name, Last Name, Occupation, etc.

    I want the user to be able to sort by column header (toggling between ASC & DESC) by clicking on any column header. And I’ve done this functionality in Javascript before.

    But the issue is how do I approach this when I have pagination enabled. I want to keep the pagination because I have over 1,000 records but I would love to code it so it sorts dynamically and keeps the sorted data while the user goes through the pages.

    Any guidance?

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

    (@bcworkz)

    This can either be managed with Ajax requests or new page requests using URL parameters to specify page, column, and direction. When the server receives the request, the request gets routed to your custom code, either as an Ajax handler or part of the page template. A new WP_Query object is created where order, orderby, posts_per_page, and offset arguments along with any other necessary arguments (e.g. post_type etc.) determine what posts to return. The critical value is offset, which will be (page number – 1) x posts_per_page.

    The query returns the posts to display on the particular page in the requested order. With Ajax, your JS clears the current container and rebuilds the new content from the server response. Naturally the template generates the appropriate output if you are not using Ajax.

    This Codex page may help you: Making Custom Queries using Offset and Pagination

    metalandcoffee

    (@metalandcoffee)

    Thank you! I ended up going with the page-reload-using-URL-parameters method. It wasn’t difficult at all once I learned of the add_query_arg( ); function and how to use _GET to pull parameters from the URL.

    I’m going to take a stab at the AJAX method since it’d made for a better UI.

    Thanks again,
    Ebonie

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! I like your attitude towards learning 🙂 You may be aware that WP Ajax has a few quirks. If you haven’t seen the Ajax section of the Plugin Handbook yet, it’s worth studying. It can be perplexing getting Ajax to initially work for you. Once you do and gain an understanding of what’s going on, the knowledge will serve you well into the future.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Column Sorting with Pagination’ is closed to new replies.