Skip to content

Instantly share code, notes, and snippets.

@yankiara
Last active January 19, 2023 01:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yankiara/4d9e6c1770897048a6072e85b058e0e9 to your computer and use it in GitHub Desktop.
Save yankiara/4d9e6c1770897048a6072e85b058e0e9 to your computer and use it in GitHub Desktop.
Allow Wordpress posts pagination on frontpage
<?php
/*
* Put following code BEFORE running the query (WP loop, Oxygen's repeater, etc.)
* What it does:
* - updates query with right page number variable to display the correct page
* - assigns the query to wp_query so that pagination links work
*/
function handle_pagination_on_frontpage( $query ) {
global $wp_query;
if ( !is_admin() && is_front_page() && $query->query['post_type'][0] == 'YOUR_POST_TYPE' ) {
$paged = get_query_var( 'page' );
$query->set( 'paged', $paged ? $paged : 1 );
$wp_query = $query;
}
}
add_action( 'pre_get_posts', 'handle_pagination_on_frontpage' );
/*
* Put following code AFTER the query
*/
remove_action( 'pre_get_posts', 'handle_pagination_on_frontpage' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment