How to add pagination for pages or cpt using WP_Query()

WordPress is designed so that you can paginate any query for posts from the database using WP_Query(), because the necessary 'paged' and 'posts_per_page' arguments are already present in the query.

By pagination we mean page navigation. For example, if we specify the output of 10 posts per page and the WP_Query() response returns information about 32 posts, pagination will show us links to page 2, 3 and 4.

And lastly – if you are in any archive template, e.g. archive.php, category.php, index.php etc., then there is already a main loop and there is no need to re-request it, in this article we are also discussing pagination for arbitrary query on the example of page type pages.

Let’s look at the code as an example:

// Get the page number of the pagination.
$current = absint( max( 1, get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' ) ) );

// Argument for the request
$args = array(
	'post_type'      => 'page',
	'posts_per_page' => 10,
	'paged'          => $current,
);		

$query = new WP_Query( $args );

if ( $query->have_posts() ) {

	echo '<ul class="posts-list">';

	// Main query.
	while ( $query->have_posts() ) {
		$query->the_post();

		echo '<li class="posts-list__item">';
			echo '<a href="' . esc_url( get_the_permalink() ) . '">' . get_the_title() . '</a>';
		echo '</li>';
	}

	echo '</ul>';

	wp_reset_postdata(); // Return the global loop.

	// Output the pagination.
	echo wp_kses_post(
		paginate_links(
			array(
				'total'   => $query->max_num_pages, // The number is taken from the default query option.
				'current' => $current, // Current page.
			)
		)
	);
} else {
	// Output a "no content" pattern if there are no posts in the query.
	get_template_part( 'templates/content', 'none' );
}

First we collect variables for the query. Here we have two mandatory arguments: 'posts_per_page' (number of posts on the page) and 'paged' (pagination page X), the rest are specified depending on the query.

Then we make a WP_Query() query, collect the data in a variable and output it, reset the custom loop and output the pagination.

In case there is no data, output templates/content-none.php.

Problem with redirect in pagination

If the pagination is rendered correctly, but when you click on a link in the pagination you get a redirect to the first page you need to disable redirect in the entries.

In WordPress, redirect is always enabled by default, but in the case of custom pagination you need to disable it.

// Turn off redirect for pages, because at /page/2 redirects to /
add_filter( 'redirect_canonical', 'disable_page_redirect' );
function disable_page_redirect( $redirect_url ) {
	if( is_page() ) {
		$redirect_url = false;
		return $redirect_url;
	}
}

Problem with 404 in the pagination

If everything is displaying correctly, but you’re catching a 404 error when you click on a post, you need to reset the link cache. Go to Settings -> Permalinks and click “Save”

How useful is the publication?

Click on a star to rate it!

Average score 4 / 5. Number of grades: 2

No ratings yet. Rate it first.

Similar posts

How to Make Different Templates for Categories and Subcategories in WordPress

In the base template hierarchy, you can use the following category templates: category-{slug}.php category-{id}.php categories.php But, if you need to apply different php templates for categories and subcategories, you can use the category_template hook and check if the current category has a parent element, in which case load e.g. subcategory.php: If your task is to…
Read more

How to exclude pages, categories, or author from search results in WordPress

By default, the search functionality in WordPress shows all record types and pages in the results. You may want to remove pages, categories, taxonomies, or posts by a specific author from your search. To do this, we need to fix the main is_search() query on the pre_get_posts hook. Let’s see some examples. How to hide…
Read more

How to change the number of entries on the search page in WordPress

By default, the number of entries on the search results page in WordPress is taken from the read settings in the admin. You can change this global request by using the pre_get_posts hook. In the example below, we’ll set it to display 50 entries on the search.php search results page:
Read more