Excluding latest posts from the loop and/or offset posts in the loop is one of the things that comes in handy when editing or designing themes. In this article we will show you how you can use the offset parameter in wp_query function to exclude latest posts from the WordPress post loop.
First you will need to find the loop that you are working with. Then you will need to add the following parameter:
query_posts('posts_per_page=5&offset=1');
This query is telling the loop to only display 5 posts which follow the most recent post (1). The important part in this code is “offset” and this magic word is doing the whole thing.
Your loop code should look like:
<?php
query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This should be it. If you have any questions feel free to ask via comments.
Source: WP Codex
WP Codex…
> “This should NOT be used within the WordPress loop”
you have been warned,
Thank you for bringing this older article to our attention
Why ? Security breach ?
WordPress added newer methods to select specific posts since the creation of this article. If you use this code in the main loop then it edits the main loop of the site is another reason that is stated in the codex.
Is there anyway for me to remove only top 3 featured post from the homepage?
How to remove all posts without featured images(thumbnails), I have 9000 to remover them!
Thanks, Good job, Good Solution
Beauty post! Simple, clear, and VERY handy. Cheers to you!
Hey guys, thanks for this article! I want to implement this, but whenever I try I’ve got an strange problem: entries would look like exactly like the frontpage, showing a list of recent post rather than the post itself. My code is slightly different, since is a SMTheme. Any help will be much appreciated. PS: Sorry for posting twice, but the code has printed wrong. Here you go:
Solution is work fine, Thanks. but pagination broke after applying this code, I have tried but not able to work with correct pagination. Any solution to pagination ? .
apply class to the li as per the number of page and a counter & give style to p1c1 as display:none;
I have used two plugins. One is “Recent facebook post” to show recent posts in wprdpress and “Facebook publish page” to publish my wordpress posts in facebook . Now I want exclude the recent post of the facebook that was already posted from my wordpress posts .
How can I do it ?
For that your plugin posting content from your Facebook page should have a check to see if a post was already published from WordPress to Facebook. We do not recommend users to directly edit plugin files. However, if you are comfortable editing php files then you can fork the plugin as a new plugin and then add the code to accomplish this.
Thanks for the great tip. I had spent hours trying to figure this out, and you made it so simple!
Thanks a lot.
the ‘post__not_in’ option does not work in wordpress 3.5
Hmm, when i tried this code on a category page loop, it couldn’t keep the posts for only that category, instead it acted as if it was the front page loop and displayed all posts.
Thank you so much man! This helped a lot!
Regards!
Thanks a lot!! very usefull tip!
Thanks!
hello, i just have one question, when i do this, pagination doesen0t work anymore, how can i fix this?
thanks, other than that, it works perfectly!!
cheers!
Yes, pagination not working. you have found any working solution for pagination, I am still searching ..
Did you ever find a solution for this?
pagination do not work on index page most so we can make out own ajax pagination call and mostly query_posts() works for pagination rather then WP_Query()
Thanks so much for this! I couldn’t figure it out at all but this worked perfectly.
Is there a way to offset a post from one cat?
E.g I want to show all posts apart from the LATEST post of category x
Yes, you can just select category_in parameter, and add your category ID.
Thanks mate, you save my day
“query_posts(‘posts_per_page=6&offset=1’);” this is not worked for me , while i am using the WP-PageNavi plugin for page navigation.
what error are you getting?
Thanks for the info. Worked well
Though, I’m encountering another issue for another site I’m currently doing. Is there a way to exclude from the loop only the latest post of a particular category?
Thing is, I’ve a blog that publishes podcasts. The front page highlights the most recent podcast – posts are published under category: Podcast – via its own styling. At the same time, the front page also shows the last 5 most recent posts, of which may include posts under the category “podcast”. I just don’t want to show in the loop the most recent post published under “Podcast” simply because it’s already featured with it’s own styling. Hope you can help me out. thanks!
You can exclude all posts from the category “postcast” from your loop if you want. But there is no simple way of excluding just the latest post.
Thanks for the tip!
But I was wondering if there is also a way to exclude the post that is currently showed on screen.
If yes, can you tell me how to do this?
Thanks in advance!
Don’t really understand your question? Which screen?
One of my pages displays first of all the latest or current post the user is viewing.
The current post is called by the loop.
Below this post I want to display the excerpts of other previous posts without the current post displayed in this list.
The previous post-excerpts are called by query_posts:
query(‘showposts=3&cat=5’);
while($previousPosts->have_posts()) : $previousPosts->the_post();
?>
As you can see I call 3 posts(excerpts) from category 5 which displays three posts from category News. This seems ok but within these 3 post-excerpts, the current post which is called by the loop is also in this list.
Do you know a way to exclude the current post within this list?
Thanks again!
You can add offset=1 in the query to skip the first post.
Thanks again for the quick response!
However, I did try the offset=1 in the query, only thing is that when you press an older post, you get to see the older post in the main loop, within the list excerpts the latest post is excluded, but the older post which is now the current post is still in the excerpts list.
Any idea how to exclude not the latest but the current post?
Thanks so far already
Email us the live link to the site, so we can see exactly whats going on. Use the contact form for email.
Thank you so much for this piece as i was looking for a way to exclude 4 latest posts form my home page and i got answer by this articles and i have replaced (query_posts(‘posts_per_page=6&offset=1’);) by query_posts(‘posts_per_page=6&offset=3’); and its done
Where exactly do you put this code to remove al posts but the sticky on the main page.
Thanks,
That is a multi-step process. First you would need to have a custom page template. Second you would need to create that custom page, your front page. Then you would run a query on that custom template showing only sticky posts.
Thanks, it was exactly what I was looking for! Thanks for any other hints.
Thanks for this. Nice solution for magazine-style layouts.