Tailor get posts from category

How i can to get all blog posts by category name or id and pass on home page?
I am new on october
Thanks

First, you need to create a relation if you already haven’t. More on that below:

Once you have that, whatever property you set to the inverse relation, lets say category, can be referenced.

So you can do posts.whereRelation('category', post.category).get()

1 Like

I am using the demo template installed with October CMS, I think there are relationships between posts and categories already set up in the demo blog.

As far as I understand, the relationship of a post to a category is defined in the schema file blog/_post-content.yaml in the section ‘categories: source: Blog\Category’.

Am I correct in understanding?

Solution found. First of all, it is necessary to create an inverse relationship as you suggested.
To add inversion, open the file blog/category.yaml. And add

posts:
    type: entries
    source: Blog\Post
    inverse: categories
    hidden: true

Next, on the page where you need to display the latest 5 posts from the ‘news’ category.

{% set categoryNewsPosts = allFromBlog.whereRelation('categories', 'slug', 'news').paginate(5) %}

Now, on the same page, we retrieve related records within a loop.

<div class="row mb-30">
    {% for allPostsByCategory in categoryNewsPosts %}
        <article class="col-lg-4 col-md-6 mb-50">
            <figure class="mb-20">

                    <a href="#"><img src="{{ allPostsByCategory.banner.path }}" alt="image for - "></a>

                    <div class="post-meta font-primary text-uppercase rotate-90 top-left">
                        <span></span>
                    </div>

            </figure>

            <div class="post-meta mb-15 font-primary text-uppercase">

                    <span class="category"><a href="#"></a></span>

            </div>
            <h4 class="post-title">
                <a href="#">{{ allPostsByCategory.title }}</a>
            </h4>
        </article>
    {% endfor %}

</div>
1 Like