0

I'm having trouble setting entries with multiple relations.

during testing I've hard coded the theme to 1 specific category

{% set themeID = "12309" %}
{% set theme = craft.categories.group('survivorThemes').id(themeID).all() %}

I then set this

{% set survivors = craft.entries.section('authors').includeInRecollection('1').relatedTo({ sourceElement: relatedAssetIds }).orderBy('lastName asc').limit(null).all() %}

and then a for loop

        {% for survivor in survivors %}
            {% set relatedIds = "" %}






{# this should work but doesn't #}
            {% set entries = 
                craft.entries.section('survivorAssets')
                .relatedTo('and', { targetElement: themeID },{ targetElement: survivor })
                .orderBy('assetOrder')
                .all() 
            %}  

{# this should also work but doesn't #}
            {% set testEntries = craft.entries.section('survivorAssets')
                .relatedTo('and', {targetElement: survivor, field: 'relatedSurvivor'}, {targetElement: themeID, field: 'themes'})
                .orderBy('assetOrder')
                .all()  %}






          <div class="testing" style="background:red;color:#fff;padding:30px;margin:30px;">

            survivor: {{ survivor.title }} <br>

            theme: {{ themeID }} <br>

            ids: {{ relatedIds}} <br>

            total results: {{ entries|length }} <br>

            test total: {{ testEntries|length }} <br>

          </div><!-- /.testing -->


                {% if entries|length %}
                    {% set activeClass = " active" %}
                    {% for entry in entries %}
                        {% set relatedIds = relatedIds ~ entry.id %}
                        {% if not loop.last %}
                            {% set relatedIds = relatedIds ~ "," %}
                        {% endif %}

              more code here
                        
                    {% endfor %}                    
                {% endif %}
                
        more code here
        
        {% endfor %}

In the above loop I expect the entries to have results but I get zero results in my test div.

survivor: Judy Abrams

theme: 12309

ids:

total results: 0

test total: 0

Have confirmed that there should be 3 entries that match the entries or testEntries variable by looking at specific entries in the control panel.

  • Does this work? {% set entries = craft.entries.section('survivorAssets').relatedTo(['and',{ targetElement: theme },{ targetElement: survivor }]).orderBy('assetOrder').all() %} – Oli Jan 27 at 16:49
  • @Oli Thank you. Can't believe I made a rookie mistake like that. Thanks for your time. – CreateSean Jan 27 at 17:14
  • Can one of you add it as an official answer? ;) – Brad Bell Jan 27 at 19:08
1

The relatedTo parameter can take an object, an element ID or an array of element objects and/or IDs. In your case the latter is expected so wrapping your arguments in an array should fix the issue:

{% set entries = craft
                .entries
                .section('survivorAssets')
                .relatedTo(['and',{ targetElement: theme },{ targetElement: survivor }])
                .orderBy('assetOrder')
                .all() %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.