<p>If you would use a custom plugin you can create this function in your (I know it's deprecated but I don't know a better name) <code>PluginVariable</code></p>

<pre><code>public function createSiteQuery(): Query
{
    return (new Query())
        -&gt;select(['id', 'groupId', 'name', 'handle', 'language', 'primary', 'hasUrls', 'baseUrl', 'sortOrder'])
        -&gt;from(['{{%sites}}']);
}
</code></pre>

<p>You can then use </p>

<pre><code>{% set sites = craft.plugin-handle.createSiteQuery().orderby({'whatever': 'you want'}).all() %}
</code></pre>

<p>And attach as many parameters to the query as you like... If you would like to fetch only sites with a specific language you can do</p>

<pre><code>{% set sites = craft.plugin-handle.createSiteQuery().where('language = "en"').all() %}
</code></pre>

<p>You have the full power of <a href="http://www.yiiframework.com/doc-2.0/yii-db-query.html" rel="nofollow noreferrer">Yii2 Query class</a>. You can group your sites as you like, change the order. You'll receive a single <code>SiteRecord</code> or multiple <code>SiteRecords</code> <a href="https://docs.craftcms.com/api/v3/craft-records-site.html" rel="nofollow noreferrer">Class reference</a> and you can display whatever information you like in the frontend. </p>

<p>You are free to change the site names in your frontend with a static array map in your php code as well. Create a map with <code>siteHandle =&gt; "new display name"</code> and do something like</p>

<pre><code>{{ craft.plugin-handle.displayCustomSiteName(site.handle) }}
</code></pre>

CC BY-SA 3.0
2018-01-12T10:48:02.243Z
Thank you! I was able to create the plugin using pluginfactory.io, and following this tuto: https://nystudio107.com/blog/so-you-wanna-make-a-craft-3-plugin.

However, I get this error while using the Twig query: Class 'vendorname\pluginname\variables\Query' not found. How should I add this Class?

Regarding the ordered list, do you have any tips to set this order in the CP? Like we would set the order of a Structure.
Romain P.
CC BY-SA 3.0
2018-01-12T13:15:20.457Z
Of course you have to include the `Query` class. If you have a good IDE it's done automatically otherwise you have to include `use craft\db\Query;` at the beginning of your file. As of now I can't think of a clean (performance wise) good way to change the order in the cp. for your sites. There is no official hook for the internal site query right now. The CP backend list is just ordered by name
Robin Schambach
CC BY-SA 3.0
2018-01-12T13:55:15.850Z
Thank you! I'm new to plugin development, also to Craft 3. Please update your example: `{% set sites = craft.sitesList.createSiteQuery().where(['=', 'language', 'en']).all() %}` is working fine (instead of `.where(['language = en'])`)
Romain P.
CC BY-SA 3.0
2018-01-12T14:11:07.523Z
Yii2 is really flexible when it comes to parameters. You can include an array or a single statement in the `where()` function. Both statements work, but I had a twig syntax error in the example above. You can see a full list of all examples what parameters to add here http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#where
Robin Schambach
CC BY-SA 3.0
2018-01-12T15:16:19.800Z
Thank you for these clarifications, but it doesn't seems to work in that case. With `{% set sites = craft.sitesList.createSiteQuery().where(['language = en"']).all() %}` I get this error: `Operator 'LANGUAGE = EN"' requires two operands.`
Romain P.
CC BY-SA 3.0
2018-01-12T15:41:29.133Z
`.where('language = "en"')` or `.where({'language': 'en'})` or `.where(['language = "en"'])` don't mix these. It's so much easier in php in my opinion... that's one reason why I don't understand why so many people stick to twig. Syntax errors are sometimes incredible hard to see
Robin Schambach
CC BY-SA 3.0
2018-01-11T12:47:13.580Z
2018-01-12T15:41:32.457Z
2018-01-12T15:41:32.457Z
Robin Schambach
<p>The Craft 3 multi-sites is very useful. However, I found the list dropdown while editing an entry confusing and awkward in my case.</p>

<p>I have several sites group, each containing a localized version of the site version:</p>

<pre><code>- International Sales
    - English version
- Production
    - English version
    - French version
— Distribution
    - English version
    - French version
</code></pre>

<p>I could have set the sites group like this, to get the right display name when looping on each sites (using <code>craft.app.sites.getAllSites()</code>):</p>

<pre><code>- International Sales
    - International Sales
- Production
    - Production
    - Production
— Distribution
    - French Distribution
    - Distribution France
</code></pre>

<p>But as you can see, there's almost nothing that inform the user which site version is using which locale, when editing the sites from the CP's dropdown.</p>

<p>So I had to change each name by something like this:</p>

<pre><code>- International Sales
    - International Sales → English
- Production
    - Production → English
    - Production → French
— Distribution
    - Distribution France → English
    - Distribution France → French
</code></pre>

<p><a href="https://i.sstatic.net/6XlHM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6XlHM.png" alt="Sites Dropdown when editing an Entry"></a></p>

<p>Now I'm facing two problems:</p>

<ul>
<li><p>How can I set an ordered list of all sites using Twig?
Because
<code>craft.app.sites.getAllSites()</code> loop through sites using an
alphabetical order, like in the CP. For example, I would like the
<em>Distribution</em> group displayed before the <em>Production</em> group.</p></li>
<li><p>How can I set a display name different from the site name? Because
<em>International Sales → English</em>, <em>Production → English</em>, etc. are ugly names, only for visual reference in the CP. I would like to
display their names like in the second version of the list:
<em>International Sales</em>, <em>French Distribution</em>, etc.</p></li>
</ul>

CC BY-SA 3.0
2018-01-10T18:56:56.717Z
For both questions, are they in the context of the Control Panel or on the front-end of the site?
Brad Bell
CC BY-SA 3.0
2018-01-11T08:41:47.810Z
Both questions are in context of the front-end.
Romain P.
<localization><entry><craft3><multi-site>
Craft 3: how can I set an ordered list of all sites, and set a display name different from the site name?
# Craft 3: how can I set an ordered list of all sites, and set a display name different from the site name?

(Asked by: username_0 on 2018-01-10)

The Craft 3 multi-sites is very useful. However, I found the list dropdown while editing an entry confusing and awkward in my case.
I have several sites group, each containing a localized version of the site version:
- International Sales
    - English version
- Production
    - English version
    - French version
— Distribution
    - English version
    - French version

I could have set the sites group like this, to get the right display name when looping on each sites (using craft.app.sites.getAllSites()):
- International Sales
    - International Sales
- Production
    - Production
    - Production
— Distribution
    - French Distribution
    - Distribution France

But as you can see, there's almost nothing that inform the user which site version is using which locale, when editing the sites from the CP's dropdown.
So I had to change each name by something like this:
- International Sales
    - International Sales → English
- Production
    - Production → English
    - Production → French
— Distribution
    - Distribution France → English
    - Distribution France → French


Now I'm facing two problems:

How can I set an ordered list of all sites using Twig?
Because
craft.app.sites.getAllSites() loop through sites using an
alphabetical order, like in the CP. For example, I would like the
Distribution group displayed before the Production group.
How can I set a display name different from the site name? Because
International Sales → English, Production → English, etc. are ugly names, only for visual reference in the CP. I would like to
display their names like in the second version of the list:
International Sales, French Distribution, etc.

**Tags:** localization, entry, craft3, multi-site
> **Comments:**
> **username_1**: For both questions, are they in the context of the Control Panel or on the front-end of the site?  
> **username_0**: Both questions are in context of the front-end.  

---

## 1 answers

### Answer by username_2 on 2018-01-11. Score: 3 | Accepted answer

If you would use a custom plugin you can create this function in your (I know it's deprecated but I don't know a better name) PluginVariable
public function createSiteQuery(): Query
{
    return (new Query())
        ->select(['id', 'groupId', 'name', 'handle', 'language', 'primary', 'hasUrls', 'baseUrl', 'sortOrder'])
        ->from(['{{%sites}}']);
}

You can then use 
{% set sites = craft.plugin-handle.createSiteQuery().orderby({'whatever': 'you want'}).all() %}

And attach as many parameters to the query as you like... If you would like to fetch only sites with a specific language you can do
{% set sites = craft.plugin-handle.createSiteQuery().where('language = "en"').all() %}

You have the full power of Yii2 Query class. You can group your sites as you like, change the order. You'll receive a single SiteRecord or multiple SiteRecords Class reference and you can display whatever information you like in the frontend. 
You are free to change the site names in your frontend with a static array map in your php code as well. Create a map with siteHandle => "new display name" and do something like
{{ craft.plugin-handle.displayCustomSiteName(site.handle) }}

> **Comments:**
> **username_0**: Thank you! I was able to create the plugin using pluginfactory.io, and following this tuto: https://nystudio107.com/blog/so-you-wanna-make-a-craft-3-plugin.  However, I get this error while using the Twig query: Class 'vendorname\pluginname\variables\Query' not found. How should I add this Class?  Regarding the ordered list, do you have any tips to set this order in the CP? Like we would set the order of a Structure.  
> **username_2**: Of course you have to include the `Query` class. If you have a good IDE it's done automatically otherwise you have to include `use craft\db\Query;` at the beginning of your file. As of now I can't think of a clean (performance wise) good way to change the order in the cp. for your sites. There is no official hook for the internal site query right now. The CP backend list is just ordered by name  
> **username_0**: Thank you! I'm new to plugin development, also to Craft 3. Please update your example: `{% set sites = craft.sitesList.createSiteQuery().where(['=', 'language', 'en']).all() %}` is working fine (instead of `.where(['language = en'])`)  
> **username_2**: Yii2 is really flexible when it comes to parameters. You can include an array or a single statement in the `where()` function. Both statements work, but I had a twig syntax error in the example above. You can see a full list of all examples what parameters to add here http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#where  
> **username_0**: Thank you for these clarifications, but it doesn't seems to work in that case. With `{% set sites = craft.sitesList.createSiteQuery().where(['language = en"']).all() %}` I get this error: `Operator 'LANGUAGE = EN"' requires two operands.`  
> **username_2**: `.where('language = "en"')` or `.where({'language': 'en'})` or `.where(['language = "en"'])` don't mix these. It's so much easier in php in my opinion... that's one reason why I don't understand why so many people stick to twig. Syntax errors are sometimes incredible hard to see  

---

