Overview

If you are using a Pager facet, this hooks lets you manipulate the paging data that the Pager uses to display itself: the currently displayed page, the number of posts per_page, the total_rows (total number of posts) and the total_pages (total number of pages).

This hook can be useful if you are manipulating the query with an offset query argument, or if you are changing posts_per_page on the fly. In these types of scenarios, this hook can be used to correct the values displayed in the Pager.

Parameters

  • $pager_args | array | An associative array of pager data (see below)

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

<?php $pager_args = [ 'page' => 1, // The page link number (integer) - Starts at 1, not 0 'per_page' => 6, // Number of posts per page (integer) 'total_rows' => 30, // Total number of posts (integer) 'total_pages' => 5 // Total number of pages (float) ];

Usage example

The following example manipulates the total_pages and per_page values used by the Pager facet:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_pager_args', function( $pager_args ) { $per_page = $pager_args['per_page']; $per_page_new = 10; $pager_args['total_pages'] = 1 + ceil( ( $pager_args['total_rows'] - $per_page ) / $per_page_new ); if ( $pager_args['page'] > 1 ) { $pager_args['per_page'] = $per_page_new; } return $pager_args; }, 10, 1 );

Note that this snippet by itself does not accomplish anything useful. This example is part of a larger piece of code that makes it possible to set a different a different posts_per_page for the first page.

See also

Last updated: July 3, 2024