Last active
November 15, 2023 06:29
-
-
Save Rupashdas/d9e4e24b227f7decc04910c1ec71d706 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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