<p>I have two channels, <code>pages</code> and <code>products</code> and category groups for <code>categories</code> and <code>regions</code>.</p>
<p>The <code>pages</code> entry type has a field where you can select entries from <code>products</code> based on their <code>category</code> and <code>region</code>.</p>
<p>The <code>products</code> entry type has fields for <code>category</code>, <code>region</code>, and <code>postcode</code>.</p>
<p>The <code>regions</code> category has a table field with a column for the region <code>name</code> and its <code>postcode</code>.</p>
<p>So at the moment I have code that displays product entries based on what's selected in the <code>categories</code> and <code>regions</code> fields.</p>
<pre><code>{% set relations = [
    'and',
    categories ? { targetElement: categories }, {# a list of category IDs #}
    regions ? { targetElement: regions } {# a list of region IDs #}
] | filter(o =&gt; o) %}

{# Include #}
{% set entries = craft.entries()
    .section('products')
    .orderBy('RAND()')
    .relatedTo(relations)
    .all() 
%}
</code></pre>
<p>Now I also need to include <code>product</code> entries whose <code>postcode</code> matches that of the <code>region</code> that's been selected.</p>
<p>I know it's probably going to include something like <code>targetElement: regions, field: 'postcodes.postcode'</code> and <code>sourceElement</code> but I can't quite get my head around it.</p>

CC BY-SA 4.0
2023-06-21T07:53:54.723Z
It's a normal table field? That's going to be difficult, table fields are difficult to filter by. Why is it a table field – are there multiple `name` + `postcode` rows in a single region category?
MoritzLost
CC BY-SA 4.0
2023-06-21T23:18:28.247Z
@MoritzLost yeah there can be multiple postcodes in a single region. Australia's a big place ;)
Tyssen
<categories><relations><relatedto>
Matching entries against category custom field
# Matching entries against category custom field

(Asked by: username_0 on 2023-06-21)

I have two channels, pages and products and category groups for categories and regions.
The pages entry type has a field where you can select entries from products based on their category and region.
The products entry type has fields for category, region, and postcode.
The regions category has a table field with a column for the region name and its postcode.
So at the moment I have code that displays product entries based on what's selected in the categories and regions fields.
{% set relations = [
    'and',
    categories ? { targetElement: categories }, {# a list of category IDs #}
    regions ? { targetElement: regions } {# a list of region IDs #}
] | filter(o => o) %}

{# Include #}
{% set entries = craft.entries()
    .section('products')
    .orderBy('RAND()')
    .relatedTo(relations)
    .all() 
%}

Now I also need to include product entries whose postcode matches that of the region that's been selected.
I know it's probably going to include something like targetElement: regions, field: 'postcodes.postcode' and sourceElement but I can't quite get my head around it.

**Tags:** categories, relations, relatedto

> **Comments:**
> **username_1**: It's a normal table field? That's going to be difficult, table fields are difficult to filter by. Why is it a table field – are there multiple `name` + `postcode` rows in a single region category?  
> **username_0**: @username_1 yeah there can be multiple postcodes in a single region. Australia's a big place ;)  

---

## 0 answers

