1

I'm wanting to show related articles based on the current article's related Markets.

How do I get the current markets and filter relevant posts based on those related markets? This is what I have so far but doesn't seem correct.

{% set allMarkets = entry.relatedMarkets.all() %}
{% set currentID = entry.id %}

{# Get Related Articles Based on Markets #}
{% set expertBlog = craft.entries()
    .section('blog')
    .orderBy('postDate desc')
    .expertOpinion(true)
    .relatedMarkets([allMarkets])
    .id(['not', currentID])
    .limit(3) %}

      <h3 class="f3-l f3-xl fw5 mt0-l mt0-xl b tc">Enjoyed this article? Keep reading more expert insights...</h3>
      <div class="flex flex-wrap cf mb4 nl3-m nr3-m nl3-l nr3-l nl3-xl nr3-xl">
          {% cache %}
          {% for entry in expertBlog %}
              {% set columns = 'w-100 w-50-m w-33-l w-33-xl' %}
              {% include "_partials/lists/blog/_cards_blog_article_authors_expert.twig" %}
          {% endfor %}
          {% endcache %}
      </div>
  • 1
    In your entry query, change .relatedMarkets([allMarkets]) to .relatedTo(allMarkets) (use relatedTo instead and you don't need brackets). – RitterKnight May 22 at 15:30
  • Thanks if you post as an official answer I will accept. – Dan Lee May 26 at 15:28
  • Dan: You bet. Done. – RitterKnight May 26 at 15:47
1

You're close!

In your entry query, change .relatedMarkets([allMarkets]) to .relatedTo(allMarkets) (use relatedTo instead and you don't need array brackets).

Final code looks like:

{% set allMarkets = entry.relatedMarkets.all() %}
{% set currentID = entry.id %}

{% set expertBlog = craft.entries()
    .section('blog')
    .orderBy('postDate desc')
    .expertOpinion(true)
    .relatedTo(allMarkets)
    .id(['not', currentID])
    .limit(3)
%}

| improve this answer | |
0

I did something similar with entry counts. The trick was to get the ids from the first query and then use that as the sourceElement for the relatedTo query.

{% set userMugQuery = craft.entries().section('userMugs').relatedTo({targetElement: entry}) %}
{% set userMugAssetQuery = craft.assets()
    .volume('userAssets')
    .relatedTo({ sourceElement: userMugQuery.ids(), field: 'userAssets' })
%}
{% set userMugCount = userMugQuery.count() %}
{% set userMugAssetCount = userMugAssetQuery.count() %}
| 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.