Custom Post TypesPHP Development

Adding Pagination Links for Custom Queried Post Types

Adding 'total' => $pastEvents->max_num_pages, argument to echo paginate_links(); function only
Wordpress Posts Pagination Screenshot

While querying for custom post types to display custom post in a page, the paginate_links() 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 WP_Query loop. This involves setting up your custom query parameters, retrieving the queried posts, and then using paginate_links() 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.

<?php
echo paginate_links( array(
       'total' => $your_wp_query_var->max_num_pages,
 ));
?>

Adding real pages link to page numbers in pagination

Adding 'total' => $pastEvents->max_num_pages, argument to echo paginate_links(); function only doesn’t create pagination that goes to exact page numbers when clicked.

You have to pass 'paged' => total_number_of_pages('paged', $default_number), argument to WP_Query class as array argument like:

The optional $default_number 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)

$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 —-

Shares:

Related Posts

Top Categories

PHP Development
22
WordPress Theme Development
21
Wordpress Development
18
WordPress JS Development
13
Show Comments (0)
Leave a Reply

Your email address will not be published. Required fields are marked *