How to exclude the first post from the query in WordPress
To exclude the first post from the query in WordPress, you can use the offset
parameter in the WP_Query
class. Here’s an example of how you can achieve this:
<?php
$args = array(
'posts_per_page' => -1, // Set the number of posts you want to display (-1 to show all)
'offset' => 1, // Exclude the first post
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display your post content here
}
} else {
// No posts found
}
wp_reset_postdata(); // Reset the query
?>
In the code above, the offset
parameter is set to 1
, which means the first post will be excluded from the query. Adjust the posts_per_page
parameter to control the number of posts you want to display.
Make sure to place this code in the appropriate template file of your WordPress theme (e.g., index.php
, archive.php
, etc.) or within a custom page template.