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
-
HTML email signature in Apple Mail
Step by step guide on how to add an HTML signature to Apple Mail.
-
Secure your WordPress Blog
A guide to securing your WordPress website.
-
HTML email signature in Gmail
How to add a HTML signature in Gmail, without any text-decoration for links
-
Remove p tags from category description
By default Wordpress adds paragraph tags to category descriptions. Find out how to stop this.
-
DIV align bottom right (or left!)
Find out how to align a DIV to the bottom left or right.
-
Website Dimensions and Designing for the Web
What browser size should you design your website. Find out more.
-
Root Path and Configuration of your Website using php
Display the root path and configuration of your website by uploading a php file to your website.
-
HTML email signature in Zoho Mail
How to add a HTML signature in Zoho Mail. A very easy and stable solution.
-
FREE EU Cookie Law Script
If you haven’t already implemented the changes then we’ve put together a little bit of code to help you.
-
Remove the shadow from Mac Screen Grabs
How to remove the shadow from Apple Screengrabs.
-
10 Useful Sites for Web Developers
Some useful websites for web developers.
-
3 FREE Apps to protect your PC
A few free apps to help you protect your PC.
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 π