<p>Any native solution is likely to be slow - I would recommend using the Preparse field plugin.</p>

<p>It lets you use Twig to dynamically generate a field at the time an entry is saved — you could have Preparse count up the number of related users on save, and then sort by that custom field.</p>

CC BY-SA 3.0
2016-04-08T12:46:27.243Z
Thanks. I ended up creating a count field which i update when new users are related.
BillyMedia
CC BY-SA 3.0
2016-04-08T13:30:15.953Z
Did you do it with Preparse? I'd recommend that over a manual text field, as the Preparse field will always recalculate based on the number of relations. The code would be as simple as: {{ entry.myRelatedUsersField|length }}
Patrick Harrington
CC BY-SA 3.0
2016-04-08T15:28:26.973Z
I read that preparse updates on entry updates, which wont work in this case as its a hidden channel storing data from an api which is populated via a custom plugin. So I added the field update to the plugin.
BillyMedia
CC BY-SA 3.0
2016-04-08T15:49:52.120Z
I believe it runs any time onSaveEntry fires, so if the custom plugin is firing the EntryModel's saveEntry method, it should run. That being said, it sounds like your way will work without issue.
Patrick Harrington
CC BY-SA 3.0
2016-04-08T00:11:15.430Z
2016-04-08T00:11:15.430Z
Patrick Harrington
<p>I have a channel with a related users field. I want to output all channel entries ordered by the number of related users, this is what i have so far:</p>

<pre><code>{%
    set entries = craft.entries.find({
        section:  'MyChannel',
        orderBy:  'relatedUsers'
    })
%}
</code></pre>

<p>What do i do to order by the number of related users?</p>

<entries><relations><ordering>
Order by related entries count
# Order by related entries count

(Asked by: username_0 on 2016-04-07)

I have a channel with a related users field. I want to output all channel entries ordered by the number of related users, this is what i have so far:
{%
    set entries = craft.entries.find({
        section:  'MyChannel',
        orderBy:  'relatedUsers'
    })
%}

What do i do to order by the number of related users?

**Tags:** entries, relations, ordering

---

## 1 answers

### Answer by username_1 on 2016-04-08. Score: 1 | Accepted answer

Any native solution is likely to be slow - I would recommend using the Preparse field plugin.

It lets you use Twig to dynamically generate a field at the time an entry is saved — you could have Preparse count up the number of related users on save, and then sort by that custom field.

> **Comments:**
> **username_0**: Thanks. I ended up creating a count field which i update when new users are related.  
> **username_1**: Did you do it with Preparse? I'd recommend that over a manual text field, as the Preparse field will always recalculate based on the number of relations. The code would be as simple as: {{ entry.myRelatedUsersField|length }}  
> **username_0**: I read that preparse updates on entry updates, which wont work in this case as its a hidden channel storing data from an api which is populated via a custom plugin. So I added the field update to the plugin.  
> **username_1**: I believe it runs any time onSaveEntry fires, so if the custom plugin is firing the EntryModel's saveEntry method, it should run. That being said, it sounds like your way will work without issue.  

---

