New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getFilteredByTags returns an empty array #1339
Comments
|
Hi @charue808 |
|
@charue808 eleventyConfig.addCollection("announcements", function (collection) {
console.log("collection all: ", collection.getAll());
const pagesToFilter = collection.getFilteredByTags("article");
console.log("pages to filter", pagesToFilter); // returning an empty array
const announcements = pagesToFilter.filter(function (item) {
return item.data.hasAnnouncement !== false;
});
console.log('announcements', announcements);
return announcements;
});This way, we should be able to determine whether the issue happens in the collection initial retrieval or whether it happens in the filtering. |
|
@samuelpath Thank you for your patience. I haven't had a chance to work on 11ty projects. I did the suggestion of collection.getAll() here is the output So it looks like getFilteredByTags works with one string but not with more? unless the format is supposed to be different but that's what the docs had... |
|
Is there an update on this? I'm experiencing this right now. I have an array called tags, and when I try to call It fails. If I pass in an array with a single value in it works, but as soon as I add a second, it fails. Something interesting I noticed as I was testing this out, I wanted to make sure I was passing the array correctly (I don't have a lot of experience with spread operators). When I execute the following code: const tags: string[] = options.tags || ['post'];
console.dir(options.tags);
console.dir(tags);
console.dir(...tags);In the console I get the following: Which seems to indicate that node isn't passing the array correctly. |
|
@samuelpath here's a repo you can use: https://github.com/johnwargo/eleventy-plugin-post-stats/tree/multiple-tags What's weird about this is that if I pass an array with only one value, it works. As soon as the array has more than one tag in it, it fails. |
|
I get an empty array if I try to access local data that doesn't have a value tag inside it. For accessing local data - as in, data returned from files within your _data directory - use the format In this code customDataCollection return array with data. Only collection created with 'tags' can be available with method getFilteredByTags or getFilteredByTag |
|
I believe that using Per https://www.11ty.dev/docs/collections/#getfilteredbytags(-tagname-secondtagname-[...]-):
UPDATE: Here's my barely tested attempt at filtering based on any tags. Basically my tags look like eleventyConfig.addCollection("catsAndDogs", function (collectionApi) {
const tagsToFilter = ["cats", "dogs"];
const pages = collectionApi.getFilteredByTag("pets");
return pages.filter(page => {
const $tags = page.data.tags ?? [];
return tagsToFilter.some(tag => $tags.includes(tag));
});
});… and because that was a terrible explanation, here's the very quick sample repo: https://github.com/pdehaan/11ty-1339 |
Describe the bug
I'm trying to use getFilteredByTags on these existing tags but but am receiving an empty array. I am using getFilteredByTag on each of those tags separately and those are generating arrays
To Reproduce
This is the code block that was written
eleventyConfig.addCollection("announcements", function(collection) {
const pagesToFilter = collection.getFilteredByTags("service", "program", "volunteer");
});
Expected behavior
I expected to return an array of objects with those tags
Screenshots
If applicable, add screenshots to help explain your problem.
Environment:
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: