webappguides
Create Wordpress Theme From Scratch! Build Your Own

How to add search pagination in WordPress

webapps log

webapps Lab
June 20, 2021
ThemeCreation

The WordPress search can show all posts of the entered search term.

Displaying every post that contains a certain word may increase page loading time.

A slow loading disrupts the user experience and a search task become tedious.

The best solution is to limit the number of results to display.

This will also require pagination.

These pages in pagination show other posts.

This post is going to show how to adopt the paginate_links function in the search.php

The paginate_links creates the numeric pagination in the search page (search.php)

Let’s get started now.

WordPress Search Pagination Code.

To paginate a search page in WordPress you need to create a function that uses $wp_query to get a number of searched results.

The function should also declare the array that defines a base URL, page format, the current page, and the total search results pages.

The function is added to a functions.php

In this snippet, the function name is a paginateSearch.

1.function paginateSearch(){

2.global $wp_query;

3.$searchPages = $wp_query -> max_num_pages;

4.$theBig = 999999999;

5.$paginateSearchArgs = array(

6.'base' => str_replace($theBig,'%#%',esc_url (get_pagenum_link($theBigNumber))),

7.'format' => '?page = %#%',

8.'current' =>  max(1, get_query_var ('paged')),

9.'total' => $searchPages

10.);

11.echo paginate_links($paginateSearchArgs);

12.}

The code alone is not enough to include pagination.

The function will show pagination if it is called in search.php

Hence, go to the desired section to call a paginateSearch.

<div class = " pagination">

<?php paginateSearch();?>

</div>

Code Explanation.

The global variable wp_query is needed to get the number of pages a search term created.

$wp_query retrieves the sum of all pages by calling the max_num_pages variable.

Check other variables that can be provoked by $wp_query.

Line-4 stores the big number integer.

I don’t know exactly the reason behind it as I saw many examples out there include it.

Let’s check the elements available in the array ($paginateSearchArgs).

Line-6 specifies the core URL of the site.

The value of the URL is obtained by esc_url which passes get_paginate_link.

According to WordPress get_paginate_link retrieves links for the specific page number.

For example, if the search term brings results that create three pages get_paginate_link creates a URL of each page.

The URL forms a link of anchor tag href attribute.

WordPress recommends esc_url when sanitizing the URL in the attribute.

Line-7 specifies the structure of the page for a search.

This post has some explanation about it WordPress Dynamic Pagination.

The next line specifies the current post.

The current value is provided by PHP max function in which two arguments are processed.

The first argument is an integer one.

The second argument is the WordPress function get_query_var.

When get_query_var passes a string ‘paged’ returns the current page pagination number.

If the current page is page 5, the max will set the current to a five.

Line-9 tells the total number of pages.

The final part of the snippet prints the numeric search pagination.

The paginate_links function returns the pages basing on the provided settings.

The setting was defined in an array.