<p>You shouldn't get random entries when no related entries were found. Relation queries add a <code>where</code> condition that limits what entries can be returned – if no related entries exist, the query will return nothing.</p>
<p>The most likely explanation is that the reviews are related to the products through some other field. In this case, you can limit the <code>relatedTo</code> query further by using the <a href="https://craftcms.com/docs/4.x/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">hash syntax</a> to only include results where the product is the <code>targetElement</code>, and also limit the query to a specific field:</p>
<pre class="lang-twig prettyprint-override"><code>{% set relatedReviews = craft.entries({
    section: 'reviews',
    orderBy: 'postDate DESC',
    relatedTo: {
        targetElement: product,
        field: 'related_product',
    }
}) %}
</code></pre>
<p>Replace <code>related_product</code> with the handle of the field on the reviews section where the product is selected.</p>
<p>For a multi-site, you can also limit the query further by <code>sourceSite</code>.</p>

CC BY-SA 4.0
2023-04-19T07:58:07.967Z
2023-04-19T07:58:07.967Z
MoritzLost
<p>I have a reviews section with an entry field to Commerce products. I'm trying to show the reverse relationship review on the product page. It appears to work when there are related entries but when there are no related entries it appears to show random results. How can I show related reviews and no others?</p>
<pre><code>{% set relatedReviews = craft.entries({
    section:        'reviews',
    orderBy:         'postDate desc',
    relatedTo:       product
}) %}

{% if relatedReviews %}
{% for relate in relatedReviews %}
   ...
{% endfor %}
{% endif %}
</code></pre>

<plugin-craftcommerce><relations>
reverse relation question in Commerce
# reverse relation question in Commerce

(Asked by: username_0 on 2023-04-18)

I have a reviews section with an entry field to Commerce products. I'm trying to show the reverse relationship review on the product page. It appears to work when there are related entries but when there are no related entries it appears to show random results. How can I show related reviews and no others?
{% set relatedReviews = craft.entries({
    section:        'reviews',
    orderBy:         'postDate desc',
    relatedTo:       product
}) %}

{% if relatedReviews %}
{% for relate in relatedReviews %}
   ...
{% endfor %}
{% endif %}

**Tags:** plugin-craftcommerce, relations


---

## 1 answers

### Answer by username_1 on 2023-04-19. Score: 1 | Accepted answer

You shouldn't get random entries when no related entries were found. Relation queries add a where condition that limits what entries can be returned – if no related entries exist, the query will return nothing.
The most likely explanation is that the reviews are related to the products through some other field. In this case, you can limit the relatedTo query further by using the hash syntax to only include results where the product is the targetElement, and also limit the query to a specific field:
{% set relatedReviews = craft.entries({
    section: 'reviews',
    orderBy: 'postDate DESC',
    relatedTo: {
        targetElement: product,
        field: 'related_product',
    }
}) %}

Replace related_product with the handle of the field on the reviews section where the product is selected.
For a multi-site, you can also limit the query further by sourceSite.


---

