How to exclude current post from recent posts

Hi,
I am trying to filter posts on an archive page and single post page so that the current post is excluded from the recent posts.

The archive page layout has the current post (on its own) at the top and recent posts underneath in a grid format, but the current post is also displayed in the recent post grid and I would like to exclude it. Also I don’t want posts to be duplicated when I add subsequent rows to the grid.

The single post page has a side bar which displays author info and recent posts related to the single post category. To filter the category I’m currently using category_name={{dc:archive:title}} in a looper provider query string to achieve this. As with the archive page the current post is also displayed in the recent post side bar and I would like to exclude it.

Could you provide me with the relevant query string conditions so that I can achieve this?

Kind regards

Dave

Hi Dave,

Thanks for reaching out.

Would you mind share us a screenshot on what you are trying to achieve? and we also need to check how you setup your layout builder. To do that, please give us the following information in a Secure Note.

  • WordPress Login URL
  • Admin level username and password

You can find the Secure Note button at the bottom of your posts.

Thank you.

Hello Dave,

Thanks for providing the information. Are you trying to display related posts using the looper? You might want to check out this thread first:

Best Regards.

Hi all,

I have asked similar question more than a month ago - in this thread. As per the last post by @alexander “exclude current post” feature was expected to be added within a few weeks. Unfortunately, it seems it is still not available. Or there is a way to achieve this somehow with a new looper provider types?

Regards
Marcin

Hello Marcin,

Our developers are still working on a solution to add a looper provider that will probably return the related posts. For now, we do not have an ETA as to when it will be release. Please do watch out for it.

Best Regards.

Thank you @ruenel.
Having related posts provided by the looper would be great, but for now exclusion of the current post would be enough. At least this is what I was asking for in my previous thread and how I understand Dave’s question.
Assuming the looper is being used in the single post layout, is it possible to exclude current post from the recent posts using query string? If yes, can you please tell us how the string should look like?

Regards
Marcin

Hi,
I to have read the post that @Marcin_Podwojski created. I was expecting this to be a simple issue to resolve based on the final comments in @Marcin_Podwojski thread. I though (rightly or wrongly) that this had been implemented, and was looking for some guidance with respect to method required to solve the problem. I understand that the development of something like Pro takes time, and that this is one request of many you must get everyday. If there is a work-around based on a query string, as Marcin suggested, this would be helpful until a more permanent solution can be put in place.

Kind regards,

Dave

Howdy, @dajanorganics! Thanks for writing in and sorry for the confusion here. I’ve checked with @alexander on this and asked him to take a look at things to see what the status might be on this feature. It is possible that the final implementation did not get rolled in to this last release due to various reasons, but we definitely plan to have something in for it down the road.

That being said, I wanted to get you a working bit of code for the time being to help tide you over. To do this, I used the same technique that I outlined in this comment on another thread:

To accomplish this, we need to create a WP_Query that can ignore any post that matches the current ID of the post being viewed. On the PHP side of things, this would involve constructing the query by passing in get_the_ID() as the ID to ignore, but we can’t utilize PHP functions in a static string of course…fortunately, this is where Dynamic Content comes in. :slight_smile: I was able to get a working string using the following in myfunctions.php file:

$query = array(
  'post_type'           => 'post',
  'posts_per_page'      => 5,
  'offset'              => 0,
  'ignore_sticky_posts' => true,
  'post__not_in'        => array( '{{dc:post:id}}' )
);

var_dump( http_build_query( $query ) );

So we’re using {{dc:post:id}} instead of get_the_ID() to get our current ID and ignore it in our recent posts query. I had to do some formatting on the Dyanmic Content string because the {, }, and : symbols get altered using the http_build_query() function, but reverting them back to their original appears to still work properly. I have tested following string locally and it appears to be working as expected:

post_type=post&posts_per_page=5&offset=0&ignore_sticky_posts=1&post__not_in%5B0%5D={{dc:post:id}}

Take that output and place it in the input for the Query String Provider Type and you should be good to go! You will want to adjust the posts_per_page value to suit your needs, and I went ahead and included the offset parameter in there as well, in case you want to utilize that. Hopefully this gets you pointed in the right direction while we work to introduce this feature in the future.

Hi @Marcin_Podwojski and @dajanorganics, I apologize, my remarks on the other thread were inaccurate. I’ve amended the comment for posterity. We’re still planning on getting the ability to exclude the current post in as an official feature, but hopefully Kory’s method above will help in the meantime.

@alexander, @kory,

Thank you so much for your efforts and for providing the workaround. Much appreciated! I was bit confused initially, but it looks that the string provided works fine by itself, without any additions to functions.php file.

Regards
Marcin

Hi @alexander, @kory,

Thank you for this. This string works great.

Just going back to my original post. I also needed a string that would do a similar thing on the archive page. Below is the section I’m referring to from the original post:

The archive page layout has the current post (on its own) at the top and recent posts underneath in a grid format, but the current post is also displayed in the recent post grid and I would like to exclude it. Also I don’t want posts to be duplicated when I add subsequent rows to the grid.

Can this be achieved in a same way as the single post page?
My login details are available in this thread if you need to take a look at my site.

Kind regards

Dave

Sure! When working in the Archive Builder there is already a provider running behind the scenes so you don’t need to add one anywhere. Inspect the Row or Section around your first post and set the Looper Consumer to “One”. This will make Dynamic Content available for the first post, but not automatically repeat the element. Then on your Grid Cell set the Consumer to “All” and it will loop over the remaining posts in the Query. This is best for performance as well because you’re just taking one post at a time from the query that WordPress already makes available (no extra DB calls).

Hi @alexander

I’ve just tried your suggestion, but unfortunately it didn’t work.
This link is to the archive I implemented it on https://dajanorganics.com/mag/your-lifestyle/food/ as you can see the posts are still repeating.

Regards

Dave

Hello Dave,

You have a different element structure as what have described by @Alexander. You should have at least this structure:

  • Section
    • Row - Looper Consumer to “One”

      Column
      Title / content/ image

    • Grid

      Cell - set the Consumer to “All”
      Title / content/ image

Hope this makes sense.

Hi @ruenel,

I tried your suggestion on the current archive layout, but unfortunately it did not work. I’m still getting repeated post in the grid layout.

I think it may be the grid layout that is causing the posts to repeat.
I currently have the following layout:
Grid 1 consists of 1 row with 2 post blocks (cell consumer set to “All”).
Grid 2 consists of 1 row with 3 post blocks (cell consumer set to “All”).
My idea was to alternate the grids and follows: 2 posts (grid 1), 3 posts (grid 2), 2 posts (grid 1), 3 posts (grid 2) etc.
This means that I have to manually insert a grid row as the archive grows, to accommodate the additional posts. But adding the extra grid rows seems to be the thing that is causing the posts to repeat.

So, my question is. Can I create a grid that will automatically grow as new posts are added, and have a 2 post (row1), 3 post (row2), 2 post (row3) etc. layout? If this is possible can you give me some guidance on how I would go about creating this grid layout.

Kind regards

Dave

Hi Dave,

That is only possible if you set the post count manually for each cell. Like you set the post count 2 for the first cell of the first grid, and 3 for the 2nd cell of the first grid.
But there is no such option for the Looper Provider to automatically grows, while new posts are added.

Thanks

Hi @tristup,

Okay, thank you.
I had that originally, but felt it was too much of a manual process.
I have an alternative layout, which I will use instead.

Kind regards

Dave

You are most welcome David.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.