Skip to content

Instantly share code, notes, and snippets.

@MaximeCulea
Created April 16, 2019 18:25
Show Gist options
  • Save MaximeCulea/481b58f8f945b7d74627a770607399be to your computer and use it in GitHub Desktop.
Save MaximeCulea/481b58f8f945b7d74627a770607399be to your computer and use it in GitHub Desktop.
Trick WP Pagenavi with a fake WP Query in order to use custom contents which are not WP ones.
<?php
/**
* Trick WP Pagenavi with a fake WP Query ¯\_(ツ)_/¯
*
* We use this method to earn time by using wp_pagenavi() method :
* - no need to reinvent the wheel, the complicated algo is already done
* - it automatically generates the html which is already stylised
* - use wp_pagenavi options from dashboard
* - whereas using custom contents which are not WP ones
*
* @url : https://fr.wordpress.org/plugins/wp-pagenavi/
*
* @author Maxime CULEA
*/
// Your customization goes here
$posts_per_page = 12;
$posts_found = 45;
// Set basics args in order to pagenavi to work
$fake_query = new WP_Query();
$fake_query->set( 'posts_per_page', $posts_per_page );
$fake_query->set( 'paged', (int) empty( get_query_var( 'paged' ) ) ? 1 : get_query_var( 'paged' ) );
$fake_query->max_num_pages = (int) ceil( $posts_found / $posts_per_page );
wp_pagenavi( [ 'query' => $fake_query ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment