<p>You can do this:</p>

<pre class="lang-twig prettyprint-override"><code>{% set query = craft.entries.section(contentSection).isFeatured('not 1') %}

{% if (categorySlug is defined) and (categorySlug != null) %}
   {# GET CONTENT FROM ONE CATEGORY #}
   {% set contentQuery = query.relatedTo(craft.categories.slug(categorySlug)).all() %}
{% else %}
   {# GET ALL CONTENT #}
   {% set contentQuery = query.all() %}
{% endif %}
</code></pre>

<p>Or if you want to be fancy ;)</p>

<pre class="lang-twig prettyprint-override"><code>{% set query = craft.entries.section(contentSection).isFeatured('not 1') %}

{% set contentQuery = ((categorySlug is defined) and (categorySlug != null)) ? query.relatedTo(craft.categories.slug(categorySlug)).all() : query.all() %}
</code></pre>

CC BY-SA 4.0
2019-06-06T19:12:03.823Z
Thanks Oli looking forward to testing this out :) :)
James Greig
CC BY-SA 4.0
2019-06-19T16:48:04.443Z
Working like a treat :thumbsup:
James Greig
CC BY-SA 4.0
2019-06-06T16:18:41.937Z
2019-06-06T16:18:41.937Z
Oli
<p>I know there's a better way to do this - eg by merging in the <code>relatedTo</code> parameter if categorySlug is defined rather than defining 2 different queries, but I can't for the life of me figure it out... how would you simplify this?</p>

<pre><code>{% if (categorySlug is defined) and (categorySlug != null) %}
   {# GET CONTENT FROM ONE CATEGORY #}
   {% set contentQuery = craft.entries
   .section(contentSection)
   .isFeatured('not 1')
   .relatedTo(craft.categories.slug(categorySlug))
%}
{% else %}
   {# GET ALL CONTENT #}
   {% set contentQuery = craft.entries
   .section(contentSection)
   .isFeatured('not 1')
   %}
{% endif %}
</code></pre>

<templating>
Neater way to merge this conditional entry query?
# Neater way to merge this conditional entry query?

(Asked by: username_0 on 2019-06-06)

I know there's a better way to do this - eg by merging in the relatedTo parameter if categorySlug is defined rather than defining 2 different queries, but I can't for the life of me figure it out... how would you simplify this?
{% if (categorySlug is defined) and (categorySlug != null) %}
   {# GET CONTENT FROM ONE CATEGORY #}
   {% set contentQuery = craft.entries
   .section(contentSection)
   .isFeatured('not 1')
   .relatedTo(craft.categories.slug(categorySlug))
%}
{% else %}
   {# GET ALL CONTENT #}
   {% set contentQuery = craft.entries
   .section(contentSection)
   .isFeatured('not 1')
   %}
{% endif %}

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2019-06-06. Score: 2 | Accepted answer

You can do this:
{% set query = craft.entries.section(contentSection).isFeatured('not 1') %}

{% if (categorySlug is defined) and (categorySlug != null) %}
   {# GET CONTENT FROM ONE CATEGORY #}
   {% set contentQuery = query.relatedTo(craft.categories.slug(categorySlug)).all() %}
{% else %}
   {# GET ALL CONTENT #}
   {% set contentQuery = query.all() %}
{% endif %}

Or if you want to be fancy ;)
{% set query = craft.entries.section(contentSection).isFeatured('not 1') %}

{% set contentQuery = ((categorySlug is defined) and (categorySlug != null)) ? query.relatedTo(craft.categories.slug(categorySlug)).all() : query.all() %}

> **Comments:**
> **username_0**: Thanks Oli looking forward to testing this out :) :)  
> **username_0**: Working like a treat :thumbsup:  

---

