Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-06T19:12:03.823Z
Answers.Text: Thanks Oli looking forward to testing this out :) :)
Answers.UserDisplayName: James Greig
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-19T16:48:04.443Z
Answers.Text: Working like a treat :thumbsup:
Answers.UserDisplayName: James Greig
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-06T16:18:41.937Z
Answers.LastActivityDate: 2019-06-06T16:18:41.937Z
Answers.UserDisplayName: Oli
Body: <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>
  
Tags: <templating>
Title: Neater way to merge this conditional entry query?
ThreadText: # 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:
  
  ```twig
  {% 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 ;)
  
  ```twig
  {% 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:  
  
  ---
  
  