Unable to pull out collection items

I have trouble with pulling out items from certain collection folders. I set a custom collection directory contents and used front matter defaults to set categories for collections stored in this folder.

There’re two groups of posts that I categorise with collections. Inside en-uk under _posts is where I store the first group, and inside en-uk of _blogs is where I store the other group.

Structure

contents
    _posts
        en-uk
            post1.md
            post2.md
            post3.md
    _blogs 
        en-uk
            post1.md
            post2.md
            post3.md

Below is how I set the front matter defaults for these collections in the _config.yml file. As can be seen, I’m using the same format for posts in the two collections.

collections_dir: 'contents'
collections:
    posts:
        output: true
    blogs:
        output: true

defaults:
-
    scope:
        path: "_posts/en-uk"
        type: "posts"
    values:
        permalink: "/:title/"
        category: "posts_en_uk"
-
    scope:
        path: "_blogs/en-uk"
        type: "posts"
    values:
        permalink: "/:title/"
        category: "blogs_en_uk"

Now, here is the challenge I’m encountering: whilst I’m able to pull out posts inside _posts/en-uk folder with {% for post in site.categories['posts_en_uk'] %}, I’ve been UNABLE to do the same for the posts inside _blogs/en-uk with the category name blogs_en_uk as set in front matter defaults. Posts inside _blogs/en-uk are NOT showing up with {% for post in site.categories['blogs_en_uk'] %} as I expect.

I don’t understand where the problem is and how to fix it. Please give me some clues. Thanks for helping out!

posts are already a special type of collection - so declaring a collection called posts is a bad idea.

posts are a special thing in jekyll and have the concept of categories and tags - nothing outside of a post will exist inside site.categories.

That said categories are not all that special, you could probably set a front matter default for ‘type’ or something and accomplish the same thing more or less. That or just use posts for everything and don’t declare the collections and you might have more luck.

@rdyar Thanks for the response. I thought when you declare a custom collection directory, according to the official documentation, you have to move _posts inside the custom directory. Do you mean I don’t need to set output: true for _posts? I don’t think that would work, but I need to test that out. And, is there a way to set categories for the collection items? I thought there is a way to do that considering that _posts is just a special type of collection. The reason I’m using category is because I’m using jekyll-paginate-v2, which requires either collection, category, tag or other properties to pull out posts and paginate, so the solution of using type won’t work.

hmm, not sure about either of those if the docs say so then that is probably correct.

Posts are a special type of collection, intermingling actual posts with a different type of collection (blogs) might not work as you intend.

If you can do the blog items as posts then I would do that. Is there is a reason you don’t want them to be posts?

Categories are a special thing for posts only - it is built in - you can still set a front matter variable of category on a non post collection item but it is not going to show up in site.categories unless it is a post. I would not use category or tag in the front matter of anything other than a post as it will be misleading I think.

@rdyar This is the page where the docs say _posts need to be moved inside the custom directory: Collections | Jekyll • Simple, blog-aware, static sites (jekyllrb.com). (Attention to the red call out titled Be sure to move drafts and posts into custom collections directory.)

I’m just hoping not to clutter the root directory. I plan to separate press releases from blog posts. I came up with this idea after having created some 100 posts, so I got to use folders to achieve this. I was thinking about a custom collection directory could help me with that. In addition to post collections, I’m also planning to create image and document collections, so not everything would fit into _posts.

I don’t know if you have used the jekyll-paginate-v2 plugin. Here is the documentation: jekyll-paginate-v2/README-GENERATOR.md at master · sverrirs/jekyll-paginate-v2 (github.com). This page lists all the properties it support to paginate posts.

I’m using categories because the collection structure is somewhat complex in my case as you can see I’m also having two languages for each type of post, and I have nested folder for those. So I thought perhaps categorising the posts is the best way to go.

I don’t use jekyll for a blog so I don’t use the paginator and don’t actually use posts normally so I don’t have experience doing what you are trying to do.

But that doesn’t change that only posts have tags and categories as an actual thing, this goes back to how jekyll was originally setup and it has causes some confusion. The paginator docs while not explicitly saying it only works for posts (filtering by cat or tag) I think this is the case. It does say you can use it for collections but does not give an example of filtering those by cat or tag - I think because they won’t show up in site.cats or site.tags and I think that is where it is getting them? it is interesting that it allows the locales front matter. Maybe you could hijack that for your own purposes if you don’t need it?

I don’t see anything about them allowing filtering by custom front matter, that is unfortunate.

The main issue you are having I think is your blogs collection is not a post, if you drop it in there maybe you would be ok? keep in mind you can set categories on posts by either front matter or path - so you can have a folder inside _posts called ‘blog’ and all posts in that folder will automatically get the cat of blog added to them. You can use that to help simplify your file organization.