How To Exclude Current Post From Loop : WP_Query ?

exclude current post from loop

While developing the front page of your website, most of the people don’t want to show the latest post. It’s because the WP_Query function is used and it is possible that the latest post may appear twice on the  front page.

It is dependent upon the loop you have developed for showing the articles. Suppose, if you are showing the posts by many categories then at two different places, the same post may appear.

To avoid this situation, you should exclude current post from loop. I am going to show you some examples of the codes in which you have to add two words.

You Can Exclude Current Post From Loop Using “offset” option.

First of all , you should have an idea about the basic loop of the WordPress which shows the articles. It contains the “if and else” statements. The “while” loop is also there.

<?php

      query_posts(‘posts_per_page=5&orderby=title’);

       if ( have_posts() ) :
       while ( have_posts() ) :

       the_post();

?>

If you find something like this then you have to add another code in the “query_posts”. Use “&” operator and add:-

offset=1

Let me show you how.

query_posts(‘posts_per_page=5&orderby=title&offset=1’);

But, every developer has his/her own style of coding and you can see some other styles of the WordPress post loop. You may notice that the blog posts are shown on the basis of any particular category.

In place of the “query_posts”, you may see some other form of coding. It is possible that the developer may use an array to attain all the information about the query post.

Let me show you an example.

<?php

     $OnePost = new WP_Query( array(‘orderby’ => ‘title’, ‘posts_per_page’ => ‘4’, ‘offset’ => ‘1’));

     if ( have_posts() ) :
     while ( have_posts() ) :

     the_post();

?>

As you can see, here an array is used passed with the WP_Query function. It is an associative array and you can define the offset here like I have shown above.

‘offset’ => ‘1’

This code is excluding the latest post from the WordPress post loop.

The main thing is to use the “offset” option. If you want to exclude current post from loop then choose this option and give the value as “1”.

But if you want to exclude more than one latest posts then you have to increase the number according to that.

Can You Now Exclude The Latest Post from Loop of WP_Query?

As I have mentioned above that while creating the front-page of your website, you may be in need to use this code.

The most common use of this code is when you show the featured post and many posts after that. It is possible that the featured post would be as same as the post shown in the list below.

To avoid this conflict, you should use the “offset” option to exclude current post from loop. If you still have any issue then feel free to ask.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, theme development, design issues and what not.

Leave a Reply

Your email address will not be published. Required fields are marked *