While querying for custom post types to display custom post in a page, the
function in WordPress primarily generates pagination links for default and regular posts associated with registered post types. However, it doesn’t create paginations for custom queried posts by default. To enable pagination for custom queried posts, you can utilize the paginate_links()
function in conjunction with a custom paginate_links()
WP_Query
loop. This involves setting up your custom query parameters, retrieving the queried posts, and then using
to generate pagination links based on the total number of posts in your custom query result. This approach includes passing array of post data to paginate_links()
function as argument.paginate_links()
<?php echo paginate_links( array( 'total' => $your_wp_query_var->max_num_pages, )); ?>
Adding real pages link to page numbers in pagination
Adding
argument to 'total' => $pastEvents->max_num_pages,
function only doesn’t create pagination that goes to exact page numbers when clicked.echo paginate_links();
You have to pass
argument to 'paged' => total_number_of_pages('paged', $default_number),
class as array argument like:WP_Query
The optional
you can pass here acts like a fallback page number if no page number is passed (which means, when the main page is visited, it will display the items from specified page number here)$default_number
$pastEvents = new WP_Query(array( 'paged' => get_query_var('paged', 1), ));
In this code I used the ger_query_var();
function and passed two arguments, paged & 1 (1 is the page number that I wanted to fallback incase the main url is visited)
—- Thankyou for Reading —-