Exclude current post from WP Query
There are times when you don’t want the current post/page you are viewing to show on the page i.e. list of recent posts, more posts from the same category and so on. Here’s how to remove that post from your query.
There are times when you don’t want the current post/page you are viewing to show on the page i.e. list of recent posts, more posts from the same category and so on. Here’s how to remove that post from your query.
In my situation I had a list of posts from a certain category that I wanted to include in the sidebar (I used the php code widget to add php to my sidebar widget) but I didn’t want the post I was currently on to be displayed in that list. So here’s the code I used:
So, in the code below “get_the_ID” is fetching the ID of the current post, “cat” is indicating the category I want to show posts from, “showposts” is indicating how many posts I would like to display and then finally “post__not_in” is getting the current post ID and then excluding it from the query.
<?php
$currentID = get_the_ID();
$my_query = new WP_Query( array('cat' => '1', 'showposts' => '5', 'post__not_in' => array($currentID)));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h2>
<?php the_content(); ?>
<?php endwhile; ?>
21 replies on “Exclude current post from WP Query”
Leave a Reply
-
Free up space on your Mac
You may not know it but there’s a lot of places on your shiny lovely Mac that can store lots of rubbish that you don’t actually need. We’ll explain a few places you can look to see if there’s a mass of files that you can safely delete and free up a few valuable gigs of space.
-
iPhone to Mac Wireless Networking
Connect to your jailbroken iPhone to your Mac to add or retrieve files and folders.
-
Is your site LIVE? FREE website monitoring sites
Keep a check on whether your website is live with these free and paid for tools.
-
Disable Flash in Chrome on Mac and PC
Sometimes you need to disable Flash. Maybe to test sites that run in both Flash and HTML and therefore automatically show you to the Flash elements or maybe you just don’t want Flash running in your browser. Whatever it is then here’s how to disable it in Chrome on a Mac.
-
Add category description if it exists
Find out how to add the category description to your category template in Wordpress.
-
iPhone Jailbreak for OS 3.0 Released
How to jailbreak your iPhone.
-
Backing up your Website using cPanel
Find out how to back up your website via cPanel
-
Domain Extensions. Are they important?
Does it make any difference what domain extension you choose? Find out more.
-
Web Browsing Tips #2: Tabbed Windows
Using tabbed windows in your web browser.
-
What's in a name? Choosing the right name for your company and website
Find out how to choose a great company name with a few basic tips.
-
Web Browsing Tips #3: Keyboard Shortcuts
Some web browsing keyboard shortcuts.
-
WordPress Login / Logout Link in Template
The standard Wordpress Login/Logout links often suffice but when you are building custom pages then the default Wordpress pages can look a little out of place on your site and therefore you may need to redirect your users to different pages and/or change the text links of those standard links.
thank you
Thanks mate
Perfect for my need. Thanks! 🙂
Very Simple Thank You….
Thanks!!
To the point, thank you
Thanks for this post. It’s still very useful.
this helped me out 🙂
thanks!
Excellent !
Could you help make the title clickable ?
Thanks,
Chris
Hi. I’ve just included this on the example above. It’s the_permalink
Great, thanks!
Thanks
Thanks Alan, just what I was looking for!
1
2
3
4
5
6
7 ‘1’, ‘showposts’ => ‘5’, ‘post__not_in’ => array($currentID)));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
Thanks for this. Worked like a charm!
tnq mod
Perfect, thanks so much.
Thanks for the code – perfect. Just watch for the — on line 3. Needed to be => for me.Â
Thanks for the heads-up. I’ve now amended the code so all should be working well.
Thanks again, simple and beautiful.
How to sort by date published (this guy is always asking for more)
thnx this has has helped me alot 🙂