My request is simple. I want to keep my blog list ordered by created_at DESC
but I want also to show the featured posts in top.
Let's say that I have 4 categories : Gold, Silver, Bronze and Other ...
I want to display the Gold posts first.
Then the Silver posts.
Followed by the Bronze posts.
And finally the others.
All of them should be ordered by created_at DESC
.
What do you think ?
I am assuming you are using the Rainlab Blog plugin. There are numerous ways to solve this and I don't think there is an official "way". Here are some examples that you will have to fit for your own code.
Solution with Twig. Twig has a sort filter which you can pass in an arrow function, check here. Then you can do if statements to display gold to bronze.
{% for blog in blogs|sort((a, b) => a.created_at <=> b.created_at) %}
{% if blog.category == Gold %}{{ blog }}{% endif %}
{% endfor %}
Adhoc CMS Page. Instead of using the rain blag component you can use the plugin in the PHP Code to the page/layout/partial. This is where give you the ability to work with the model to organize it the way you want to. Here I am using OctoberCMS querying features.
use Rainlab\Blog\Models\Post;
public function onStart() {
$this['golds'] = Post::whereHas('categories', function ($query) {
$query->where('name', 'Gold');
})->get()->sortBy('create_at');
}
Third way and the way I recommend is to build your own plugin which can either extend or filter the blogs posts like how I did in the CMS Page example. Read the documentation here.
One side note that if you click into the {% component 'something' %}
you can expand the htm template.