Pagination in Genesis custom loop
Most of the times, I am using custom loops in templates for my customers. And of course, custom queries with WP_Query class. Normally, I create a new query such as
$the_query = new WP_Query( $args );
I guess it’s the habit since I was coding child themes of Automattic default themes. Nothing wrong with it. Until I tried to use default pagination in Genesis custom loop, with genesis_posts_nav() function. It is not working!
A quick search showed me a snippet from Bill Erickson. Ok, it looks exactly the same way I was doing. What’s wrong?
The real reason is that the pagination in Genesis uses $wp_query, not $the_query. Meanwhile, in my custom template, I only used $the_query, and called to a function using the global variable $wp_query.
A simple fix is changing in my template, the variable $the_query to $wp_query. Voilà! The pagination function is working now. With few more settings in CSS, I can have beautifully functioned paginated pages.
Another important note is the need of overwriting wp_query. That is done with
$wp_query = new WP_Query( $args );
If not doing that, the number of pages in pagination will be wrong.