Genesis Custom Loop with Pagination

Ever needed a Genesis Custom Loop with Pagination? Frustrating, isn’t it?

Here is a snippet to set you up:

https://gist.github.com/billerickson/3218052/

You can use it in page templates, archives, custom post type archives (archive-{cpt}.php), etc.

Edit: the snippet was improved by Bill Erickson as per his comment, the gist above is now his gist. The old one is here and refers to Bill’s snippet.

Reader Interactions

Comments

  1. Here’s my recommended changes to the code: https://gist.github.com/3218052

    I really don’t like seeing tutorials that use query_posts since 99.99% of the time it is misused (yours is that 0.01%). query_posts overwrites the main query mid-way through the page. Most of the time you want a custom query or you want to modify the main query.

    The only reason you’d want to overwrite the main query mid-way is if you want to use a core WP function in your custom query, and that core WP function uses $wp_query. The pagination functions use $wp_query, which is why it’s needed in this instance.

    So instead of using query_posts(), I’m creating a custom query that overwrites $wp_query. Again, it’s the same result, but as a tutorial it shows people how to build custom queries so hopefully they’ll do that instead of using query_posts() in all instances.

    Also, there’s no need to remove genesis_posts_nav() from the genesis_after_endwhile hook, then add it back to your custom query. Just add the genesis_after_endwhile hook in your custom query. This way you maintain the flexibility of selecting your pagination method from Genesis> Theme Settings

    • Great comment Bill and you are absolutely right. I was thinking too much about pre_get_posts and how that screwed up a sidebar ajax content filter widget I’m working on. I’ve edited the post and embedded your improved gist.

  2. I just wanted to relate that this totally worked and helped a php newb like me out, as all the custom post type remedies I found didn’t work, I guess because I have a super neat Genesis Framework child theme…

    A quick modification of the code here got me up and running with working pagination using the genesis_posts_nav() tag. 🙂

    Thanks again.

  3. Hi I have an html dropdown menu to select from a list of custom post types and I save the selected value in a php variable. However when I insert the variable into your loop it returns the desired number of records per page for the selected post type but only on the first page. When the next page link is clicked the variable holding the selected post type returns empty. I can’t seem to work out what I need to do with the post type submitted through my form in order for it to retain its value while the pagination changes. Any help much appreciated. Best Bis

  4. Hi,

    Thanks for giving away this away for free!

    I have an additional question. Your code is working perfect on subpages. But on the root (homepage) the pagination is visible, but not working properly. Previous is redirecting to root and on third page same posts as on home are visible. Do you have any clue what it can be?

    Your thoughts are much appreciated! Many thanks in advance!

    Regards,
    Stefan

    • Something is probably overwriting your query, look for anything that uses query_posts for instance.

  5. Thanks for this.
    I was having trouble with the last page of pagination turning up blank. I figure including global $wp_query; before declaring the new, custom query did the job at overwriting the post count and resetting it.

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.