Last active
July 18, 2018 00:26
-
-
Save elvismdev/9fce9eb87f8aee9b306616feb2155c96 to your computer and use it in GitHub Desktop.
WordPress: Exclude the most recent post from the main query without braking pagination.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // ... | |
| /** | |
| * Tweak Archive pages queries. | |
| * @param object $query | |
| * @return object | |
| */ | |
| function tweak_archives( $query ) { | |
| // If this is a Story Type term archive page. | |
| if ( !is_admin() && is_tax( 'story_type' ) && $query->is_main_query() ) { | |
| // Exclude the latest post from the main query without braking pagination. | |
| $ppp = get_option( 'posts_per_page' ); | |
| $offset = 1; | |
| // Detect and handle pagination... | |
| if ( $query->is_paged() ) { | |
| // Determine page query offset (offset + current page (minus one) x posts per page) | |
| $page_offset = $offset + ( ( $query->query_vars['paged'] - 1 ) * $ppp ); | |
| // Apply adjust page offset | |
| $query->set( 'offset', $page_offset ); | |
| } else { | |
| // This is the first page. Just use the offset... | |
| $query->set( 'offset', $offset ); | |
| } | |
| } | |
| return $query; | |
| } | |
| add_filter( 'pre_get_posts', 'tweak_archives' ); | |
| /** | |
| * Adjusting the offset pagination on the Story Type taxonomy archive. | |
| * @param int $found_posts | |
| * @param object $query | |
| * @return int | |
| */ | |
| function adjust_offset_pagination( $found_posts, $query ) { | |
| $offset = 1; | |
| if ( !is_admin() && is_tax( 'story_type' ) && $query->is_main_query() ) { | |
| $found_posts = $found_posts - $offset; | |
| } | |
| return $found_posts; | |
| } | |
| add_filter( 'found_posts', 'adjust_offset_pagination', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment