5 minute read
August 16, 2017

Exclude current entry from collection list in Statamic

Being able to show a list of related entries is a fairly common requirement for modern websites. For example, you may have a collection of case studies. It's a great idea to show other projects from the same type of work at the bottom of that case study. The same idea applies to other types of content, like blog posts or tutorials, like this very entry.

Outputting a list of entries in Statamic

The first thing we need to do is output a list of all entries on your Statamic site. To do this, we'll use the collection tag.

All entries in Statamic are part of a collection. If you've recently started using Statamic and come from a WordPress background, think of entries as posts and collections as a way of grouping your content together – like posts and pages.

The collection tag allows us to tell Statamic that we want to access one of our collections. We then need to tell Statamic which collection we want to retrieve entries from.

For the sake of our example, let's imagine we're wanting to display a list of related projects and exclude the current project. To keep things simple, our collection is therefore called 'Projects'. Also, so we actually see something, we're going to print out the title of each entry using the {{ title }} tag.

As such, the most basic code we would use be:


{{ collection from="projects" }}
  {{ title }}
{{ /collection }}

This will loop through all of our entries in the 'projects' collection and print out each entry's title. As you will have noticed, the title of the entry you're on will have been printed out, as well as the title of every other entry in that collection.

Filtering out the current entry

If we want to exclude the current entry, we need to identify a unique attribute that we can use to single it out. The easiest thing to target is the value of the entry's URL.

To do that, we can target the url by adding a url parameter to the code. We can then use Statamic's built in conditions to say we only want to return entries if they are not equal to the value we pass in.

To do this, we will add url:not={ value } to the existing parameter of from="projects"

Finally, before we add that code to the collection tag, we need to add the appropriate value. For something like the URL, it's pretty obvious (it's url, surprise, surprise).

However, if you're working with something more obscure and you're not sure of what is value to use, you can use the ever useful {{ dump }} tag. Simply add it to your file and it'll print out every available key/value available to the page you're on. (Just remember to remove the tag once you're done.).

So now we know we are checking the value of URL, we can add that in. That gives us the following code:


{{ collection from="projects" url url:not="{ url }" }}
  {{ title }}
{{ /collection }}

If you refresh the page, you'll see that you now have a list of all your entry's titles, minus the title of the page you're currently on.

You can read more about filtering in Statamic's documentation.

Taking it further: Filtering out by taxonomy

The project I was working on used this approach to filter posts by taxonomy term, rather than by the collection itself.

For example, at the bottom of an case study that is tagged with the term 'Special Projects', I want all other case studies in that taxonomy to appear. To minimise the amount of work the client has to do when inputting data, I also wanted the filter to work automatically in the background.

The taxonomy filter works similar to the examples we've seen so far. The main difference is that you call the taxonomy filter parameter and tell it what taxonomy you want to filter by.

To do this, you would add this code:

taxonomy:projects"

Taking it further, each case study is assigned a term within the projects taxonomy – for example 'Special Projects'.

Now we could easily add an additional field within the fieldset for our Projects collection, but that would be adding an additional step for the end user. It would also open up to possiblity of them forgetting to add it and it breaking the site (we'd use a required field of couse, but the point is it opens up the possibility of issues).

So ideally we want to call this dynamically based on the data available within the entry. If you use our old trusty friend {{ dump }} again, we can identify the value of the key that is assigned to our category.

In this case it's projects_raw. If you have any other taxonomies set up, you'll see it follows the pattern of taxonomy name followed by _raw. So if your taxonomy was called categories, the key would be categories_raw.

If we add that key as our value to the code before, we get this:

taxonomy:projects="{ projects_raw }"

Now we can place that code into our collections tag.


{{ collection from="projects" url url:not="{ url }" taxonomy:projects="{ projects_raw }  }}
  {{ title }}
{{ /collection }}

That will now return all entries from our Projects collection that are not the current entry and have the same taxonomy term associated with it as the current post.