Skip to content

Instantly share code, notes, and snippets.

@Clorith
Created July 17, 2014 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Clorith/c3276277e1b75c217262 to your computer and use it in GitHub Desktop.
Save Clorith/c3276277e1b75c217262 to your computer and use it in GitHub Desktop.
<?php
function set_page_for_post( $query ) {
/*
* Only run our query modifications if;
* This is the main query (not a custom WP_Query or similar)
* We are not on the admin screen
* It's a single post entry
* The post type is 'post'
*/
if ( $query->is_main_query() & ! is_admin() && is_single() && 'post' == get_post_type() ) {
$page_value = isset( $_GET['random_var'] ) ? $_GET['random_var'] : false;
if ( ! $page_value || $page_value == 'all' || false === intval( $page_value ) ) {
return $query;
}
// Set the paged value for the query, and the query_vars, this is the pagination variable used by WP
$query->query['paged'] = $page_value;
$query->query_vars['paged'] = $page_value;
// Define this query as a paginated entry
$query->set( 'is_paged', true );
// Return our modified query
return $query;
}
}
add_filter( 'pre_get_posts', 'set_page_for_post' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment