47
votes
Accepted
Section entries - order by random
You can use MySQL's RAND() function inside your order method:
{% for entry in craft.entries.section('team').type('mainPeople').order('RAND()') %}
27
votes
Accepted
How can I return data from a Matrix field in random order?
You can either have MySQL select the blocks in a random order using RAND():
{% set blocks = entry.myMatrixField.order('RAND()') %}
Or you can shuffle the blocks from Twig instead, using Craft’s ...
18
votes
Accepted
Ordering Entries without using entry date
I'd suggest changing the Recipe section to a "Structure" section type.
This lets the client order the entries with a drag and drop from the Entries tab where they can see the whole list of entries ...
13
votes
Accepted
Order entries by date field
Read the article about ElementCriteriaModel in the official Craft docs on how to query elements in general and the craft.entries article about the parameter available to modify the object returned.
...
12
votes
From a template, how can you order entries by one field, and if that field is empty a different field?
There are several ways you could attack this, but probably the easiest is directly from the SQL side with something like this using a SQL CASE statement:
{% set entries = craft.entries.section('news')...
12
votes
Accepted
Output children of a structure section in reverse order
The primary parameter you're looking for is lft:
craft.entries.section('volumes').order('lft desc')
9
votes
Accepted
Order entries by category field
Here's a solution using the group filter.
{% set entriesGrouped = craft.entries.relatedTo(category).order('title')|group('myNewCategoriesField.first().title') %}
{% for group in entriesGrouped %}
...
9
votes
Accepted
How to order or filter entries by their author's custom field?
This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query.
We first perform an inner join on the author's content table using the authorId so that the ...
8
votes
Output children of a structure section in reverse order
You could try using use the reverse twig filter.
{% for entry in craft.entries.section('volumes')|reverse %}
...
{% endfor %}
8
votes
Accepted
No ordinary news post ordering
You can do this entirely from the order param using MySQL’s CASE syntax:
{% set newsItems = craft.entries({
section: 'news',
order: '(CASE field_myLightswitchField
WHEN 1 ...
8
votes
How to order by matrix field?
My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
7
votes
Order tags by count - e.g. Top 10 Tags Used
This is based on Matt's answer to your previous question. Likewise this should better be used within cache tags!
This is untested, but it hopefully works out for you.
.
Edit:
This is still ...
7
votes
Accepted
Order Assets by date field
You're so close it's maddening—you should be able to order by a custom field as well:
{% set assets = craft.assets.find({sourceId: 2, order: 'date desc'}) %}
Where date is the name of your custom ...
6
votes
Accepted
Ordering of ElementCriteriaModel with varying statuses
Your statuses are incorrect. They would be correct for many elements, but the Entry Element overwrites them to the following:
EntryModel::LIVE
EntryModel::PENDING
EntryModel::EXPIRED
BaseElementModel:...
6
votes
Accepted
How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?
Give this a whirl, Mark:
{% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
5
votes
Accepted
Order entries by "recently modified" date time in control panel
Short answer is, you can't.
Long answer... Ian has some good advice, but the Post Date won't update automatically. Your best solution would be to update the Post Date every time you update something ...
5
votes
Accepted
Sorting categories by the post date of the entries related to them
Alan,
Without having tested this, you might get the desired result by using the group filter.
So, get all you entries ordered by dateCreated, and then group them by category:
{% set sermons = craft....
5
votes
Accepted
How to order by matrix field?
Your example is referencing a field that Twig doesn't know about yet. Also, you usually place the fields you are ordering entries with in quotes (ex: order('dateField')).
Matrix blocks are elements (...
5
votes
How to order numbers naturally?
I think the simplest solution is to use a plugin like Twig Better Sort or SuperSort. With those you would query for your entries as normal and then apply the Twig filter to sort the array.
{% set ...
5
votes
Accepted
Change field arrangement from the entries page
No, that's not possible with Craft at the moment (2.4). You'll have to use the Field Layout Designer from Settings -> Sections -> {Section} -> {Entry Type} to manage your fields and tabs.
You could ...
5
votes
Accepted
How to order Sites
I've seen that before, if you look at the sites table in your DB, you'll see that a few sites might have their sortOrder set to 1.
If you go to Settings → Sites → [the offending group] in your ...
4
votes
No ordinary news post ordering
I don't know if it's possible (but would like to see a query solution).
There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it ...
4
votes
Accepted
Reverse Assets fixedOrder
You could try twig's reverse filter.
{% for asset in entry.assetsField|reverse %}
<img src="{{ asset.url }}" alt="{{ asset.title }}"/>
{% endfor %}
4
votes
How Do I group entries by Category and retain fixed Category Order?
Depending on your needs, you may be able to ditch the group() filter and sort your list however you need.
Get all your entries and categories. Then use the relatedTo functions to filter those lists.
...
4
votes
Accepted
Sort / Order Entries on select or button click
Okay so you can either do this through ajax or through a page reload, a page reload is going to be much easier, but you can get it to work without reloading the page...
Reloading the page
You can ...
4
votes
Accepted
Output entries sorted by category title and paginate
Not exactly what you're looking for, but it gets you closer. This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% ...
4
votes
Order entries based on date field in the Control Panel
The CpSortableCustomColumns plugin lets you sort your element index tables on custom fields. Sounds like it does exactly what you want.
4
votes
Order/sort a structure section by date too?
This should work
{% set entries = craft.entries({
section: 'news',
sortOrder: 'postDate desc, lft asc',
}) %}
4
votes
Ordering entries based on an eager-loaded element's custom field
This requires a bit of extra work on the ElementQuery, but it can be done in the same query.
We need to perform an inner join on the relation between the movie and the show and then another inner ...
3
votes
No ordinary news post ordering
My SuperSort plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
So, you ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
ordering × 87entries × 25
templating × 12
order × 11
categories × 10
relations × 10
sorting × 10
datetime × 8
matrix × 7
craft3 × 6
structure × 5
sections × 5
group × 5
assets × 4
plugin-craftcommerce × 4
tags × 3
query × 3
relatedto × 3
sort × 3
plugin-development × 2
control-panel × 2
entry × 2
cache × 2
elementcriteriamodel × 2
filter × 2