<p>You've got your two obvious choices nailed down.  You're either offloading the filtering to MySQL (running 3 separate queries so that MySQL filters the data) or you're grabbing all of the data and filtering it on the Twig/PHP side.</p>

<p>Which one is more performant likely depends on 1) how much data you're talking about and 2) your environment (resources allocated to MySQL vs. PHP).</p>

<p>For your particular case, I'd enable <a href="https://craftcms.com/docs/config-settings#devMode" rel="nofollow">devMode</a> and analyze the performance metrics between the two methods while taking into account how fast the data might increase in the future and make an educated guess based on that information.</p>

CC BY-SA 3.0
2016-09-06T06:35:45.213Z
And how would I do this in Twig? The first thing think of is looping through all entries and do an if/else in the loop (`{% if playerPosition = "goalie" %}do stuff{% endif %}`). Would that be correct?
Bart Rylant
CC BY-SA 3.0
2016-09-06T17:49:53.987Z
@BartRylant yup, that's what I was thinking!
Brad Bell
CC BY-SA 3.0
2016-09-06T04:30:32.483Z
2016-09-06T04:30:32.483Z
Brad Bell
<p>Is there a kind of “search in results after you retreived all entries” thing? Thing is: I have this query:
<code>craft.entries.section('players').limit(null)
</code>
I want to show all entries where <code>playerPosition</code> = goalie. And on the same page, I want to show all players where <code>playerNationality</code> = Nederlands. And on the same page I want to show the longest player (sorted by <code>playerHeight</code>).</p>

<p>I can off course run a query 3 times:</p>

<pre><code>goalies = craft.entries.section('players').playerPosition('goalie').limit(null)
nationalplayers = craft.entries.section('players').playerNationality('Nederlands').limit(null)
longest = craft.entries.section('players').order('playerLength desc').first();
</code></pre>

<p>That would work, but I'm running a query 3 times on the same section. There must be a better way, right? – Some have told me about the SuperSort plugin (which was indeed the right solution for my <a href="https://craftcms.stackexchange.com/questions/16489/order-by-fields-after-searching">previous question</a>), but that only <em>sorts</em> results. It's not a real filter.</p>

<entries><search><filter>
Filter results after searching
# Filter results after searching

(Asked by: username_0 on 2016-09-05)

Is there a kind of “search in results after you retreived all entries” thing? Thing is: I have this query:
craft.entries.section('players').limit(null)

I want to show all entries where playerPosition = goalie. And on the same page, I want to show all players where playerNationality = Nederlands. And on the same page I want to show the longest player (sorted by playerHeight).
I can off course run a query 3 times:
goalies = craft.entries.section('players').playerPosition('goalie').limit(null)
nationalplayers = craft.entries.section('players').playerNationality('Nederlands').limit(null)
longest = craft.entries.section('players').order('playerLength desc').first();

That would work, but I'm running a query 3 times on the same section. There must be a better way, right? – Some have told me about the SuperSort plugin (which was indeed the right solution for my previous question), but that only sorts results. It's not a real filter.

**Tags:** entries, search, filter


---

## 1 answers

### Answer by username_1 on 2016-09-06. Score: 1

You've got your two obvious choices nailed down. You're either offloading the filtering to MySQL (running 3 separate queries so that MySQL filters the data) or you're grabbing all of the data and filtering it on the Twig/PHP side.

Which one is more performant likely depends on 1) how much data you're talking about and 2) your environment (resources allocated to MySQL vs. PHP).

For your particular case, I'd enable [devMode](https://craftcms.com/docs/config-settings#devMode) and analyze the performance metrics between the two methods while taking into account how fast the data might increase in the future and make an educated guess based on that information.

> **Comments:**
> **username_0**: And how would I do this in Twig? The first thing think of is looping through all entries and do an if/else in the loop (`{% if playerPosition = "goalie" %}do stuff{% endif %}`). Would that be correct?  
> **username_1**: @BartRylant yup, that's what I was thinking!  

---

