Do you want to exclude or skip current post from the WP Query? So here is a small WordPress snippet which is that allows you to exclude or skip the current WP post from the WP Query, For example you can try or use on list of recent posts, related post, or more posts from the same category etc. What the snippet does, is getting the ID of the current post and then use this post ID to exclude the current post form being included in the query.
INSTRUCTIONS
Simply copy paste or add the following code in to your WordPress theme template files where you usually use your queries and you can modify code as per your needs i.e. ‘showposts’ => ‘4’, ‘cat’ => ‘1’. etc
$excludeCurrentID = get_the_ID();
$query = new WP_Query( array(‘showposts’ => ‘4’, ‘cat’ => ‘1’, ‘post__not_in’ => array($excludeCurrentID)));
while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><a href=”#”><?php the_title() ?></a></h2>
<?php the_content(); ?>
<?php endwhile; ?>
I hope this code will help you.
In the end
I hope you have found this article helpful. Let us me your opinion or questions if any through the comment form in below or use this form to ask your question.