Contents
Can you use WP _ query instead of pagination?
Pieter Goosen’s answer is completely correct, and his suggestion to use WP_Query instead makes the most sense. However, I stumbled across this question whilst looking for pagination with get_posts outside of the loop, so I thought this might also be a useful alternative for anyone else:
How do I get pagination to work for get _ posts ( )?
I do think that next_posts_link () and previous_posts_link () is better to use with a custom query, ie with WP_Query. You must just remember however to set the $max_pages parameter when you make use of a custom query, otherwise your pagination will break
When to use WP _ query instead of global?
In the example above, the custom WP_Query object $query is used instead of the global $wp_query object. This code is for Custom Query Pagination. You can follow the steps to create your own pagination in WordPress.
What’s the difference between paged and offset in WP _ query?
get_posts has a direct property called offset which achieves pretty much the same thing as paged in WP_Query; but where paged refers to pagination (e.g. 1, 2, 3 ), offset is the actual number of posts you want to offset your query by (e.g. 5, 10, 15 ). With a little maths – numberToShow * pageNumber – you can get the correct offset easily:
When to use the paged parameter in a custom query?
When the query is fetched, that paged parameter will be used to determine which set of paginated posts to return. If a displayed pagination link is clicked, and the next page loaded, your custom query won’t have any way to know that the pagination has changed.
How to fix pagination for custom loops in WordPress?
If you merely want to modify the parameters for the main loop query – such as changing the posts per page, or excluding a category – you may be tempted to use query_posts (). But you still shouldn’t. When you use query_posts (), you force WordPress to replace the main query object.