<p>You could use a <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow">Preparse field</a> to calculate and store that value.</p>

<p>The only downside is, it'll only do that calculation when you save the <strong>parent</strong> entry. So if you add another descendant, you'd have to go back and re-save the parent entry to re-calculate the value.</p>

<blockquote>
  <p><strong>Pro tip:</strong> You can go to <code>Settings &gt; Sections</code> and re-save the section you are working with. This will automatically trigger a re-save of every single entry in that section.</p>
</blockquote>

CC BY-SA 3.0
2016-03-31T21:33:27.407Z
I'd like to upvote with half a point for this half baked solution. But as it's probably the only way to approach that problem without custom sql, I'll give you the full upvote. :)
carlcs
CC BY-SA 3.0
2016-03-31T21:37:19.267Z
Lol, thanks @carlcs. It's the only way I could think of without introducing something like template hooks.
Lindsey D
CC BY-SA 3.0
2016-04-01T18:22:38.350Z
I did come across the Preparse Field plugin while searching for a solution. Since I want this process to be automated, I’m guessing my best bet it to write a custom plugin where I latch on to a hook and trigger a re-save on the parent everytime an entry is modified in that section.
rungta
CC BY-SA 3.0
2016-04-01T19:51:17.123Z
@rungta: That sounds like a good solution!
Lindsey D
CC BY-SA 3.0
2016-03-31T17:48:55.847Z
2016-03-31T17:48:55.847Z
Lindsey D
<p>I want to sort (and paginate) entries from a structure section based on the number of descendants. This is the effective criteria I want to query:</p>

<pre><code>craft.entries.section('myStructure').level(1).order('descendants desc')
</code></pre>

<p>Since <code>getDescendants.count()</code> is not a number stored against each entry in the database (AFAIK), how does one go about getting this result?</p>

<structure><sorting>
How to sort entries by their descendant count?
# How to sort entries by their descendant count?

(Asked by: username_0 on 2016-03-31)

I want to sort (and paginate) entries from a structure section based on the number of descendants. This is the effective criteria I want to query:
craft.entries.section('myStructure').level(1).order('descendants desc')

Since getDescendants.count() is not a number stored against each entry in the database (AFAIK), how does one go about getting this result?

**Tags:** structure, sorting

---

## 1 answers

### Answer by username_1 on 2016-03-31. Score: 2 | Accepted answer

You could use a [Preparse field](https://github.com/aelvan/Preparse-Field-Craft) to calculate and store that value.

The only downside is, it'll only do that calculation when you save the **parent** entry. So if you add another descendant, you'd have to go back and re-save the parent entry to re-calculate the value.

> **Pro tip:** You can go to `Settings > Sections` and re-save the section you are working with. This will automatically trigger a re-save of every single entry in that section.

> **Comments:**
> **username_2**: I'd like to upvote with half a point for this half baked solution. But as it's probably the only way to approach that problem without custom sql, I'll give you the full upvote. :)  
> **username_1**: Lol, thanks @carlcs. It's the only way I could think of without introducing something like template hooks.  
> **username_0**: I did come across the Preparse Field plugin while searching for a solution. Since I want this process to be automated, I’m guessing my best bet it to write a custom plugin where I latch on to a hook and trigger a re-save on the parent everytime an entry is modified in that section.  
> **username_1**: @rungta: That sounds like a good solution!  

---

