eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Is there a way to find a tag in an indivudal collection instead of all collections?

Open distinctperspective opened this issue 5 years ago • 3 comments

I have 2 collections...one called post and one called profiles.

collection.posts collection.profiles

I want to be able to find the tag featured in each one not both. Is there a way to do that?

would I have to create a custom collection like "featuredpost" or "featuredprofiles"??

  // Collections
  eleventyConfig.addCollection("posts", function (collection)  {
    return collection.getFilteredByGlob("src/_posts/*.md").sort(function(a, b) {
        return b.date - a.date;
    });
  });

  eleventyConfig.addCollection("profiles", function (collection)  {
      return collection.getFilteredByGlob("src/_profiles/*.md").sort(function(a, b) {
          return b.date - a.date;
      });
  });

How would that be done???

Here is the site: https://staging.areyoumodelmaterial.com/

As you can see here they are mixed https://staging.areyoumodelmaterial.com/news/category/featured/

distinctperspective avatar Apr 25 '20 04:04 distinctperspective

Not certain I understand your issue – if you want the top section to, say, only have posts but not profiles, you could do this by adding a tag either to the front matter for each post or by adding a posts.json file inside the folder with "tags" : "posts".

Then in your template file or the file for this landing page use {% for post in collection.posts %} add html for each post here {% endfor %}. By default posts sort by date.

Does that help?

peterwgnd avatar May 29 '20 15:05 peterwgnd

Can't you use the getFilteredByTag function: https://www.11ty.dev/docs/collections/#getfilteredbytag(-tagname-)

shanerobinson avatar May 30 '20 18:05 shanerobinson

I will try that @peterwgnd. @shanerobinson I don't think that will work. I will try and make a better explanation thanks.

distinctperspective avatar Jul 12 '20 07:07 distinctperspective

You can filter arbitrarily however you’d like using the Collections API.

See: https://www.11ty.dev/docs/collections-api/#example-get-all-filter

eleventyConfig.addCollection("posts", function (collection)  {
    return collection.getFilteredByGlob("src/_posts/*.md").filter(item => 
        return item.data.tags.includes("included") && !item.data.tags.includes("excluded");
    });
});

zachleat avatar Nov 21 '25 17:11 zachleat