Sort entries by global variable category I'm picking a season category (spring, summer, fall, winter)  in a global variable that should dictate what entries come to the top of non-season category pages. These entries are already on a category page, sorted by their main category. I need extra sorting to showcase the "Spring" entries, for instance. Something like this:

Without the season global variable chosen


Test post 1 (Summer)
Test post 2 (Spring)
Test post 3 (Spring)
Test post 4 (Winter)
Test post 5 (Winter)


With the global variable "Spring" chosen


Test post 2 (Spring)
Test post 3 (Spring)
Test post 1 (Summer)
Test post 4 (Winter)
Test post 5 (Winter)


Basically, I want to bring the entries that are using the chosen Global Variable Category to the top, the remaining entries can be sorted by postDate descending. These entries are in a Structure btw, if that matters.

Structure = 'generalContent'
Category Field = 'season'
Global Variable = 'season.season'

Simplified code of my category page.

{% paginate craft.entries.section('generalContent').relatedTo(category).order('lft desc') as entries %}
  {% for entry in entries %}
    {{ entry.title }}&lt;br&gt;
  {% endfor %}
{% endpaginate %}
One solution is to retrieve the entry ids for the season entries and all entries separately; subtract the 'season entry ids' from 'all entry ids' (using craft's without twig filter); then merge the two arrays back together (using twig's merge filter) in the proper order. You can then use this new id array to generate an elementCriteriaModel which can be used in your paginate expression.

{% cache %}
    {% set season = season.season.first() %}
    {% set seasonEntryIds = craft.entries.section('generalContent').relatedTo(season).order('lft desc').ids() %}
    {% set allEntryIds = craft.entries.section('generalContent').order('lft desc').ids() %}
    {% set nonSeasonEntryIds = allEntryIds|without(seasonEntryIds) %}
    {% set sortedEntryIds = seasonEntryIds|merge(nonSeasonEntryIds) %}

    {% paginate craft.entries.id(sortedEntryIds).fixedOrder(true) as entries %}
        {% for entry in entries %}
            {{ entry.title }}&lt;br&gt;
        {% endfor %}
    {% endpaginate %}
{% endcache %}


Since there are a lot of queries happening here, it might also be a good idea to wrap the code in 'cache' tag.