Skip to content

Instantly share code, notes, and snippets.

@Rupashdas
Last active November 15, 2023 06:29
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 Rupashdas/d9e4e24b227f7decc04910c1ec71d706 to your computer and use it in GitHub Desktop.
Save Rupashdas/d9e4e24b227f7decc04910c1ec71d706 to your computer and use it in GitHub Desktop.
<?php
function terrawise_exclude_first_post_from_query($query) {
if (!is_admin() && $query->is_main_query() && (is_home() || is_category())) {
if (!$query->get('exclude_first_post')) {
$first_post = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
));
if (!empty($first_post)) {
$first_post_id = $first_post[0]->ID;
$query->set('post__not_in', array($first_post_id));
$query->set('exclude_first_post', true);
}
}
}
}
add_action('pre_get_posts', 'terrawise_exclude_first_post_from_query');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment