<?php if ( $products_query->max_num_pages > 1 ) : ?>
// Bring $wp_query into the scope of the function
// Backup the original property value
$backup_page_total = $wp_query->max_num_pages;
// Copy the custom query property to the $wp_query object
$wp_query->max_num_pages = $products_query->max_num_pages;
<div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
<div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
// Finally restore the $wp_query property to it's original value
$wp_query->max_num_pages = $backup_page_total;
<?php endif; ?>
The problem is that when I am on page 1, I can see the next_posts_link, but when I am on page 2 there is still only the next_posts_link and no previous_posts_link (the HTML markup is there, but the link is missing). Even when I’m on the last page, there is only the next_posts_link (although there are no next posts) and no previous_posts_link.
I’ve read many threads with similar/same problems, but I can’t solve it. This post helped me to show the navigation at all, before both links where missing.
My test site is here, to see it scroll down, the pagination has a red background.
Can somebody help or point me in the right direction?
You are basically trying to fake the posts_link functions into thinking your query is the main query. You need to do more than copy over the max_num_pages value. I can’t condone trying to fool functions, but you could try copying your entire query object to the global, after backing it up of course. No guarantees that will work.
The correct approach is to either make your query the main query via the ‘pre_get_posts’ action or to handle your own pagination the same way the posts_link functions do. It’s mainly a matter of calculating the proper OFFSET parameters for the next query based on page number and posts per page.