{exp:channel:entries channel="ranking" order="points" sort="desc"}
{team} | {points} | {goals_scored} | {goals_against} 

The code works fine,but when there are two teams locked at same points I want the Goal difference to take over the responsibilty of ranking teams.I do have the goal_scored and goal_against field.

How do I do this?

  • By Goal difference, do you mean {goals_scored}-{goals_against}?
    – Sobral
    Jun 2, 2014 at 15:04
  • @RobsonSobral : yes,exactly.
    – user2133
    Jun 2, 2014 at 18:32
  • Check the new answer, please.
    – Sobral
    Jun 2, 2014 at 18:57

2 Answers 2


If by "Goal difference" you mean {goals_scored} - {goals_against}, you gonna have to write a custom query.

With the Channel Module, the closer you can get is to sort both fields in different directions:

{exp:channel:entries channel="ranking" orderby="points|goals_scored|goals_against" sort="desc|desc|asc"}
{team} | {points} | {goals_scored} | {goals_against}

Or, for a perfect result, code your own query:

    ct.title AS event_title, 
    ct.url_title AS event_url_title,
    ct.entry_id AS entry_id,
    cd.field_id_ID1 AS team,
    cd.field_id_ID2 AS points,
    cd.field_id_ID3 AS goals_scored,
    cd.field_id_ID4 AS goals_against,
    cd.field_id_ID3 - cd.field_id_ID4 AS goal_difference
FROM exp_channel_titles AS ct
JOIN exp_channel_data AS cd ON ct.entry_id = cd.entry_id
WHERE ct.channel_id = 2
AND ct.expiration_date NOT BETWEEN 1 AND unix_timestamp(Now())
AND ct.entry_date <= unix_timestamp(Now())
ORDER BY points DESC, difference DESC
        {team} | {points} | {goals_scored} | {goals_against}

I added some fields that could be useful and prevented to show expired or future entries. You still need to change the IDs of the fields.

  • Exactly.but will this give me perfect results?
    – user2133
    Jun 2, 2014 at 17:26
  • @Vagabond, this is a different logic, but, for your question, "Order considering two fields", this is the right answer. For what you really need, "order by the subtraction of two fields", you have to code a custom query. An easy query, actually.
    – Sobral
    Jun 2, 2014 at 18:03

Another option if available in your build would be to use Stash. You could simply build a list containing the following for each ranking

  • points
  • team
  • goals_scored
  • goals_against
  • goals_difference (which you could calculate using something like mx calculator

You could then use multiple orders and sorts (amongst which the goals difference) when using {exp:stash:get_list}

Not tested but something along the lines of:

Build list

{exp:stash:set_list name="st_rankings" parse_tags="yes"}
    {exp:channel:entries channel="ranking" disable="categories|category_fields|member_data|pagination" dynamic="no"}
        {stash:item_goals_difference}{exp:mx_calc expression="{goals_scored} - {goals_against}"}{/stash:item_goals_difference}

Output list

{exp:stash:get_list name="st_rankings" orderby="item_points|item_goals_difference" sort="desc|desc" sort_type="numeric|numeric"}
    <h2>Team: {item_team}</h2>
    <p>Points: {item_points}</p>
    <p>Goals scored: {item_goals_scored}</p>
    <p>Goals against: {item_goals_against}</p>
    <p>Goals difference: {item_goals_difference}</p>
  • just went through stash and what it can do..I posted my answer.Still,i ll be greatful if you can show me a bettr way
    – user2133
    Jun 4, 2014 at 13:40
  • Added some code to get you started (although Robson gave you a working solution already). Jun 4, 2014 at 20:47
  • Wow.Thx for the response.
    – user2133
    Jun 5, 2014 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy