You need to do a little “hack” to get pagination to work for your custom loop.
After you define $loop
, do the following:
<?php
// globalize $wp_query
global $wp_query;
// copy $wp_query into a temporary variable
$temp_wp_query = $wp_query;
// nullify $wp_query
$wp_query = null;
// move $loop into $wp_query
$wp_query = $loop;
?>
At this point, your posts_nav_link()
should work as expected.
Now, after the loop, swap the original object back into $wp_query
, so that everything else on the page that is query-dependent will work properly:
<?php
// restore original $wp_query
$wp_query = $temp_wp_query;
?>
Related Posts:
- How to make my pagination loop continuously?
- next_post_link for CPT only working for a few posts? weird!
- Why do next pages load the same content rather then older articles?
- Displaying the author of next and previous posts in pagination?
- Need help with get_posts pagination
- How to determine if I’m on the first page of pagination?
- How To create ajaxed wp-pagenavi?
- pagenavigation not showing the next page just linking back to the main page [closed]
- How do I insert CSS into a function?
- Pagination with $_POST and $_SESSION
- Next and Previous Posts of Same Parent
- Wp-pagenavi giving page not found on clicking page 2
- How to exclude or filter password protected posts when using next_post_link() previous_post_link
- query_posts pagination will always show identical content
- WordPress post pagination continuous [duplicate]
- WP Page Navi. 404 error when using posts per page parameter [duplicate]
- Pagination Links Not Showing Up on single.php posts on iOS Devices
- WP_Query and pagination AGAIN?
- remove rel=next on category page (if
- Page navi for template page
- Can’t Add Pagination
- Why next_posts(); function does not work inside header.php?
- WordPress not showing pagination links
- previous_posts_link returns null when querying custom post type
- pagenavi shows a lot of pages calculated from all posts in the site with query_posts
- Need help getting this page nav working correctly [closed]
- Next / Previous Posts not working
- pagination/prev and next page links not showing
- Pagination with wp_pagenavi not working on custom page
- post pagination names, not numbers
- How to fix pagination after rewriting url? ie. www.site.com/players/type/pro/page/3/
- Custom WP_Comment_Query with pagination and orderby?
- Pagination: How do I always show ‘previous’?
- How to use next_post_link and previous_post_link on single posts in search results
- Varying the number of posts per page from the first one
- Using pagination with get_posts on page type
- specify meta_key / meta_value condition for prev_post_link and next_post_link
- Using previous_post_link and next_post_link to wrap around post sequence
- Static front page with recent posts pagination
- Custom taxonomy list and sortby letter pagination problem
- previous / next post with thumbnail
- Custom Page Template for “Older Posts” Pages
- How to change title tags on paginated posts?
- Post in Multiple Categories to stay in current category (permalink, next previous post link)
- paginate_links() outputs extra empty pages on custom loop
- When to check for max_num_pages using WordPress REST API?
- Adding pagination to a custom archive template
- WordPress pagination link always leads to home page
- Get attachment next and previous by author only
- WordPress Pagination not working, any ideas?
- Altering next_posts_link(); and previous_posts_link();
- Prev and Next link for splitting single post into multiple pages [duplicate]
- Pagination: /tags/tag_name/?paged=2 gives a 404 error
- Wpdb result with pagination
- Pagination indication “/page/2/” are removed from url and auto redirect without /page/2/ : 301 redirection
- Pagination with no ‘page’ slug
- Modify WP_List_Table pagination “item(s)” text?
- How to stop WordPress returning 404 on paginated pages after using add_rewrite_rule()?
- Invalid previous pagination links generated from paginate_links()
- next_post_link and previous_post_link order not working properly
- Pagination customization
- Pagination w/Static Front Page
- How do I fix this ‘Undefined variable:’ debug message?
- How do I paginate a list of categories?
- Post Pagination Modifications (wp_link_pages)
- /tag/tag_name/page/2 gives a 404 error
- How can I change pagination numbers with next and back button?
- Post Navigation Not Working In Blog Template – Redirecting to same page with correct URI
- How to get rest api pagination headers via fetch
- Can I use paginate_links() to add a pagination list, yet prevent it from inserting links?
- Page links are not working after moving domain [closed]
- Get next post in same category not working WP 4.7.4
- How to remove /page/2/ from home page?
- wordpress custom search page pagination returns 404
- Change the format in the paginage_links
- Pagination on WP_User_Query not updating current page
- Blog pagination WITHOUT using max_num_pages
- paginate posts on admin page
- Pagenavi Plugin and Custom Post Type – Multipage results
- WordPress paginate_links function: How do i ensure the page number is not less than 1 and not greater than last
- Pagination on page 2+ only
- How to replace pagination with Next/Previous for multi page posts?
- How to Add Pagination to Page?
- Disable and redirect pagination of Home
- How to hide empty pagination button when no previous page
- Pagination not working on homepage, On other pages it is working
- next and previous category name and link parent only wordpress in archive page
- WP_User_Query counter not updating with pagination
- I would like help getting my pagination to work
- Display Both next previous and number in single page [duplicate]
- Need help on wordpress pagination script
- Pagination link problem when reaching the second page
- Add next and previous post links on pages like category, tags or archive pages
- Stop loading more posts if none left AJAX
- Wp Pagenavi how to display all results
- Multiple next / previous
- How to add a class and title attribute to the link generated by next/previous post
- pagination leads to 404 page
- How to automatically load more data when users reaches at the bottom of the page of the wordpress site?
- Next/previous post link to posts only within one or more specific categories