<p>You can use either query parameters or a second URL segment to have both the store and the country available for filtereing. For example, let's say your base URL for a store page is something like this: <code>example.com/barnesnoble/</code>. You can set your links for countries to either of those:</p>
<pre><code>example.com/barnesnoble/us
example.com/barnesnoble/?country=us
</code></pre>
<p>Then in your template, you can find both the current store and the selected country:</p>
<pre><code>{% set store = craft.app.request.segments|first %}

// option 1 (segments)
{% set country = craft.app.request.getSegment(2) %}

// option 2 (query parameter)
{% set country = craft.app.request.get('country') %}

{% set countryCategory = country ? craft.categories().slug(country).one() : null %}
</code></pre>
<p>Which of those approaches is better depends a bit on what filtering options you want to provide. If you have a very rigid structure (for example, the store is always selected first, after that you can further narrow the results by one country), URL segments are preferrable. But if you want your filters to be more flexible, query parameters are easier to extend. For example, based on your previous question, if you want your visitors to be able to select multiple categories, that can be easily solved with query parameters:</p>
<pre><code>example.com/barnesnoble/?country[]=us&amp;country[]=uk
</code></pre>
<p>Then adjust your template to get multiple country categories to filter by:</p>
<pre><code>{% set country = craft.app.request.get('country') %}
{% set countryCategories = country ? craft.categories().slug(country).all() : null %}
</code></pre>
<h2>Update: How to output and route custom links</h2>
<p>Outputting a link which includes both the store's slug and the country's slug as an additional segment or URL parameter is just a matter of appending both slugs together. Here's an example using query parameters:</p>
<pre><code>{% set store = craft.app.request.segments|first %}
{% set storeCategory = craft.categories()
                        .group('stores')
                        .slug(store)
                        .one()
%}

{# Output links for country filters #}
{% set countries = craft.categories().group('countries').all() %}
{% for country in country %}
    {% set url = url(storeCategory.slug ~ '/' ~ country.slug) %}
    &lt;a href=&quot;{{ url }}&quot;&gt;{{ country.title }}&lt;/a&gt;
{% endfor %}
</code></pre>
<p>If you're using URL segments, you have to define a <a href="https://craftcms.com/docs/3.x/routing.html#advanced-routing-with-url-rules" rel="nofollow noreferrer">dynamic route</a> to make sure those links don't result in a 404 page.</p>
<pre class="lang-php prettyprint-override"><code>// routes.php
return [
    '&lt;storeSlug:{slug}&gt;/&lt;countrySlug:{slug}&gt;' =&gt; ['template' =&gt; 'path/to/store.twig'],
];
</code></pre>
<p>Now you just need to adjust your template a bit. Instead of accessing <code>craft.app.request</code>, craft passes the named URL parameters directly to your template. Based on the example above, you would have access to the variables <code>storeSlug</code> and <code>countrySlug</code> which you can use to get the actual categories.</p>
<pre><code>{% set store = storeSlug is defined
    ? craft.categories().section('stores').slug(storeSlug).one()
    : null
%}
{% set country = countrySlug is defined
    ? craft.categories().section('countries').slug(countrySlug).one()
    : null
%}
</code></pre>

CC BY-SA 4.0
2021-07-30T18:50:32.017Z
that's help me! I have a very rigid structure, so yes the store is always first, maybe I even can use a combination of two, segments and search but! now my BIG question is, how can I get the URL like: example.com/barnesnoble/us, why? is easy get the URL like that when you have just one category group, so US is the child and the parent is Barnes, but remember I have TWO separate category groups, so when I hit the store title button I got /barnes or /us when hit the country button, but I can't get /barnes/us when I hit the country, overall I lose! the "father" (barnes) when I hit the country.
user15280052
CC BY-SA 4.0
2021-08-02T08:11:39.983Z
@user15280052 You can just append the store and country slug together to get a URL like `/barnesnoble/us/`. Then define a [dynamic route](https://craftcms.com/docs/3.x/routing.html#dynamic-routes) to make sure those URLs go to your store template instead of a 404 page. I've updated my answer with an example!
MoritzLost
CC BY-SA 4.0
2021-07-29T07:53:04.473Z
2021-08-02T08:13:48.703Z
2021-08-02T08:13:48.703Z
MoritzLost
<p>This is related with <a href="https://craftcms.stackexchange.com/questions/37913/split-categories-by-levels-list-categories-merged-by-name">Split categories by levels / List categories merged by name</a> which I explain more on detail there, after that I take the suggestion of create separate category groups, so:</p>
<ul>
<li><p>I have a Section type: Channel called <strong>Products</strong></p>
</li>
<li><p>I have two category groups: <strong>Stores</strong> and <strong>Countries</strong></p>
</li>
</ul>
<ol>
<li>I have sidebar with the Stores listed (I already solved that), when I click on a Store, I want list all entries that are tied to that brand and also the countries tied to those entries.</li>
</ol>
<p><a href="https://i.sstatic.net/SQU5R.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/SQU5R.png" alt="enter image description here" /></a></p>
<ol start="2">
<li>And when I click in a country, I want KEEP! the Store title the selected country and list all the products that are tied to that country and store, like this:</li>
</ol>
<p><a href="https://i.sstatic.net/t55Sh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/t55Sh.png" alt="enter image description here" /></a></p>
<p>I bet all this can be resolved with <a href="https://craftcms.com/docs/3.x/relations.html" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/relations.html</a></p>
<p>I try solved this with the url slug,
<code>{% set label = craft.categories.slug(craft.app.request.segments|first).one() %}</code>
/barnes/us but I getting /barnes or /us only so I lose the store title, any idea how to resolve this?</p>

<categories><filter><filtering>
Filter entries based multiple category groups
# Filter entries based multiple category groups

(Asked by: username_0 on 2021-07-29)

This is related with [Split categories by levels / List categories merged by name](https://craftcms.stackexchange.com/questions/37913/split-categories-by-levels-list-categories-merged-by-name) which I explain more on detail there, after that I take the suggestion of create separate category groups, so:

* I have a Section type: Channel called **Products**
* I have two category groups: **Stores** and **Countries**

1. I have sidebar with the Stores listed (I already solved that), when I click on a Store, I want list all entries that are tied to that brand and also the countries tied to those entries.

[![enter image description here](https://i.sstatic.net/SQU5R.png)](https://i.sstatic.net/SQU5R.png)

2. And when I click in a country, I want KEEP! the Store title the selected country and list all the products that are tied to that country and store, like this:

[![enter image description here](https://i.sstatic.net/t55Sh.png)](https://i.sstatic.net/t55Sh.png)

I bet all this can be resolved with <https://craftcms.com/docs/3.x/relations.html>

I try solved this with the url slug,
`{% set label = craft.categories.slug(craft.app.request.segments|first).one() %}`
/barnes/us but I getting /barnes or /us only so I lose the store title, any idea how to resolve this?

**Tags:** categories, filter, filtering

---

## 1 answers

### Answer by username_1 on 2021-07-29. Score: 1

You can use either query parameters or a second URL segment to have both the store and the country available for filtereing. For example, let's say your base URL for a store page is something like this: example.com/barnesnoble/. You can set your links for countries to either of those:
example.com/barnesnoble/us
example.com/barnesnoble/?country=us

Then in your template, you can find both the current store and the selected country:
{% set store = craft.app.request.segments|first %}

// option 1 (segments)
{% set country = craft.app.request.getSegment(2) %}

// option 2 (query parameter)
{% set country = craft.app.request.get('country') %}

{% set countryCategory = country ? craft.categories().slug(country).one() : null %}

Which of those approaches is better depends a bit on what filtering options you want to provide. If you have a very rigid structure (for example, the store is always selected first, after that you can further narrow the results by one country), URL segments are preferrable. But if you want your filters to be more flexible, query parameters are easier to extend. For example, based on your previous question, if you want your visitors to be able to select multiple categories, that can be easily solved with query parameters:
example.com/barnesnoble/?country[]=us&country[]=uk

Then adjust your template to get multiple country categories to filter by:
{% set country = craft.app.request.get('country') %}
{% set countryCategories = country ? craft.categories().slug(country).all() : null %}

Update: How to output and route custom links
Outputting a link which includes both the store's slug and the country's slug as an additional segment or URL parameter is just a matter of appending both slugs together. Here's an example using query parameters:
{% set store = craft.app.request.segments|first %}
{% set storeCategory = craft.categories()
                        .group('stores')
                        .slug(store)
                        .one()
%}

{# Output links for country filters #}
{% set countries = craft.categories().group('countries').all() %}
{% for country in country %}
    {% set url = url(storeCategory.slug ~ '/' ~ country.slug) %}
    {{ country.title }}
{% endfor %}

If you're using URL segments, you have to define a dynamic route to make sure those links don't result in a 404 page.
// routes.php
return [
    '/' => ['template' => 'path/to/store.twig'],
];

Now you just need to adjust your template a bit. Instead of accessing craft.app.request, craft passes the named URL parameters directly to your template. Based on the example above, you would have access to the variables storeSlug and countrySlug which you can use to get the actual categories.
{% set store = storeSlug is defined
    ? craft.categories().section('stores').slug(storeSlug).one()
    : null
%}
{% set country = countrySlug is defined
    ? craft.categories().section('countries').slug(countrySlug).one()
    : null
%}

> **Comments:**
> **username_0**: that's help me! I have a very rigid structure, so yes the store is always first, maybe I even can use a combination of two, segments and search but! now my BIG question is, how can I get the URL like: example.com/barnesnoble/us, why? is easy get the URL like that when you have just one category group, so US is the child and the parent is Barnes, but remember I have TWO separate category groups, so when I hit the store title button I got /barnes or /us when hit the country button, but I can't get /barnes/us when I hit the country, overall I lose! the "father" (barnes) when I hit the country.  
> **username_1**: @user15280052 You can just append the store and country slug together to get a URL like `/barnesnoble/us/`. Then define a [dynamic route](https://craftcms.com/docs/3.x/routing.html#dynamic-routes) to make sure those URLs go to your store template instead of a 404 page. I've updated my answer with an example!  

---

