<p>You'll probably want to read up a bit on <a href="https://docs.craftcms.com/v3/relations.html#templating" rel="nofollow noreferrer">relations</a> in the Craft docs, but I think something like this should work</p>

<pre><code>{# First get the category elements #}
{% set supportCategory = craft.categories().id(458).one() %}
{% set widgetCategory = craft.categories().id(459).one() %}

{# Now we use those elements in our query #}
{% set entries = craft.entries().section('Post').relatedTo(['and',
  { element: supportCategory },
  { element: widgetCategory }
]).all() %}

{% for entry in entries %}
  {# ... your code #}
{% endfor %}
</code></pre>

<p>I believe the relation needs an element, but you might be able to do something like <code>.relatedTo(['and', 458, 459])</code>.</p>

CC BY-SA 4.0
2019-10-21T03:37:44.183Z
2019-10-21T03:37:44.183Z
brianjhanson
<p>Thank you brianjhanson, you got me 95% there and pointed me to my final solution.</p>

<p>My entry.slug for each page is the exact title of the tag I want to relate to, so this is the code I settled on.</p>

<pre><code>{% set supportCategory = craft.categories().id(458).one() %}
{% set productTags = craft.tags().title(entry.slug).one() %}

{# Now we use those elements in our query #}
{% set entries = craft.entries().section('Post').relatedTo(['and',
   { element: supportCategory },
   { element: productTags }
]).orderBy('postDate desc').limit(4).all() %}
</code></pre>

CC BY-SA 4.0
2019-10-22T00:54:27.790Z
2019-10-22T00:54:27.790Z
ChrisE
<p>I am trying to find the last four Channel entries (Named: Post) with a Category relatedTo "Support" (id: 458) AND Tag relatedTo: "Widget1" (id: 469)
I've had success using Category or Tag but not both:
This Craft Query works for Category</p>

<pre><code>for entry in craft.entries.section('Post').relatedTo('458').orderBy('postDate desc').limit(4).all()
</code></pre>

<p>How can I add the dynamic Tag to the Craft query? The dynamic Tag matches the slug of the current page "widget1" or slug of another page "widget2".</p>

<p>Thank you.</p>

<craft3><categories><tags>
Craft Query by Category and Tag
# Craft Query by Category and Tag

(Asked by: username_0 on 2019-10-20)

I am trying to find the last four Channel entries (Named: Post) with a Category relatedTo "Support" (id: 458) AND Tag relatedTo: "Widget1" (id: 469)
I've had success using Category or Tag but not both:
This Craft Query works for Category
for entry in craft.entries.section('Post').relatedTo('458').orderBy('postDate desc').limit(4).all()

How can I add the dynamic Tag to the Craft query? The dynamic Tag matches the slug of the current page "widget1" or slug of another page "widget2".
Thank you.

**Tags:** craft3, categories, tags

---

## 2 answers

### Answer by username_1 on 2019-10-21. Score: 1 | Accepted answer

You'll probably want to read up a bit on relations in the Craft docs, but I think something like this should work
{# First get the category elements #}
{% set supportCategory = craft.categories().id(458).one() %}
{% set widgetCategory = craft.categories().id(459).one() %}

{# Now we use those elements in our query #}
{% set entries = craft.entries().section('Post').relatedTo(['and',
  { element: supportCategory },
  { element: widgetCategory }
]).all() %}

{% for entry in entries %}
  {# ... your code #}
{% endfor %}

I believe the relation needs an element, but you might be able to do something like .relatedTo(['and', 458, 459]).


---

### Answer by username_0 on 2019-10-22. Score: 1

Thank you brianjhanson, you got me 95% there and pointed me to my final solution.
My entry.slug for each page is the exact title of the tag I want to relate to, so this is the code I settled on.
{% set supportCategory = craft.categories().id(458).one() %}
{% set productTags = craft.tags().title(entry.slug).one() %}

{# Now we use those elements in our query #}
{% set entries = craft.entries().section('Post').relatedTo(['and',
   { element: supportCategory },
   { element: productTags }
]).orderBy('postDate desc').limit(4).all() %}


---

