6

The issue with your current code is that you're looping over each type/size category, creating a parameter in your relatedTo query for each one. This means that you're currently creating a query that looks like this: relatedTo([ 'and', { targetElement: 1 }, { targetElement: 2 }, { targetElement: 3 }, { targetElement: 4 } ]) ...which, ...


6

To exclude the current post use the current post's id with a "not" clause in the id query param. {% for relatedPost in craft.entries.relatedTo(entry.newsCategory).id('not ' ~ entry.id).limit(5).all() %}


4

Something like this should work: {% set tags = craft.tags.limit(null).relatedTo(craft.entries.limit(null)).order('title') %} {% for tag in tags %} <a href="/tags/{{ tag.slug }}">{{ tag.title }} (craft.entries.relatedTo(tag).total())</a> {% endfor %} Note that if you have a significant amount of tags, the above could be a costly affair, ...


4

You first need to query for all post entries and then query for the authors that are related to a post. You can use the ids() method to get just the author’s element IDs, which is a lot quicker than find(). {% set allPostIds = craft.entries({ section: 'posts', limit: null, }).ids() %} {% set people = craft.entries({ section: 'people', ...


3

If you're only trying to find related articles based on elements (ie tags, categories, entries, etc), you can use my Similar plugin. Here's a basic example, more info in the repo: {% set similarEntriesByTags = craft.similar.find({ element: entry, context: entry.tags }) %} <ul> {% for similarEntry in similarEntriesByTags %} <li>{{ ...


3

You're being a little too specific in your entries call. Chances are you don't have any entries at level 3. And, what happens if you have entries in a channel that might be related to those categories? {% for cat in craft.categories.group('group1') %} <p>{{ cat.title }}</p> {# You can just query all the entries in the system. Or you can ...


3

Your code is only listing entries in the structure that are nested inside 2 parent entries. Try this: {% for entry in craft.entries.section('structure').relatedTo(cat) %} Also, your <ul> is inside your for loop, it should be: <ul> {% for entry in craft.entries.section('structure').relatedTo(cat) %}` <li>{{ entry.title }}</li&...


3

Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any). {# loop through each of the categories #} {% for category in craft.categories.group('eventsCategories') %} {# find the exhibitor entries that are related to both the Event entry and category #} {% ...


3

Second relatedTo call overrides first one. Calling it again won't add more parameters to the query. If you don't want to repeat yourself you can always do this: {% set creatorRelated = { targetElement: creatorEntry, field: 'creator' } %} {% set caseEntries = craft.entries.section('case').relatedTo(creatorRelated) %} {% set caseEntries = caseEntries....


3

If I understand you correctly, you want to exclude any entry that has any user from a certain user group selected in a certain Users field. Here's an approach that I believe would work: {# Get the users that should be excluded #} {% set excludedUsers = craft.users.group('groupToExclude').limit(null) %} {# Get the entries that should be excluded #} {% set ...


3

There's a typo in your code :) You misspelled relatedTo. I'm assuming that's the issue because other that than, the code looks like it should work.


3

I think what you want here is the union of (products related to a or b) and (products related to c, d, or e). One good way to do these more complicated queries is to construct a set of the ids, and then do your final query based on that. So, we will get the ids of products related to a or b: {% set aOrBProductIds = craft.commerce.products({ relatedTo: ...


3

It does not work because the function does not accept slugs. Not sure what to answer here. It only accepts objects of type Element and ids as identifier. You can see the magic here foreach ($elements as $element) { if (is_numeric($element)) { $relElementIds[] = $element; } else if ($element instanceof ElementInterface) { $relElementIds[] = $element-...


3

Something like this should work: {% set myStation = getCookie('myStation') %} {% set relatedStation = craft.entries.id(myStation) %} {% set params = { section: 'blog', limit: 6, offset: 1, relatedTo: relatedStation } %} {% if category is defined %} {% set params = params|merge({relatedTo: ['and', { element: relatedStation }, { element: ...


3

$page = Entry::find() ->relatedTo([ 'targetElement' => $article, 'field' => 'articleSections.articles' ]) ->section('page')->all(); Please read the docs about ElementQueries and relations Note: the Element API Creates an ElementQuery from your criteria property. criteria => [ 'sectionId' => 4, ...


3

{% set people = craft.users.group('family').ids() %} {% set relatedPlaces = craft.entries.section('locations').relatedTo({ targetElement: person, field: 'relatedPeople' }) %} Grab all the ids from your users, then find all locations related to those. This way you don't need multiple loops and get each location once and not multiple times


3

Answer for Craft 2 Your final result should not look like relatedTo('and', '1,2') but relatedTo('and, 1, 2') Or relatedTo(['and', 1, 2]) So you just need to do relatedTo('and,' ~ param) Keep in mind to code defensively, so you might want to check if your query param is not null or something Answer for Craft 3 relatedTo(['and', ['element' => 1], [...


3

This should do it: {% set eventCats = craft.request.getParam('cat')|split(',') %} {% set events = craft.entries.relatedTo(['and']|merge(eventCats)).find() %}


3

That field type doesn’t currently exist, but it wouldn’t be hard to create it. Here’s the relevant code you’d need if you want to take a stab at it: use Craft; use craft\base\ElementInterface; use craft\commerce\models\Discount; use craft\commerce\Plugin as Commerce; // ... public function normalizeValue($value, ElementInterface $element = null) { if ...


3

Indigo Viking's code will achieve what you want, but if you want a simpler solution (possibly a bit more performant since there's no second for loop), you could do this instead: {% set suitability = craft.categories.group('suitability').all() %} {% for category in suitability %} <dt class="data-list__item data-list__item--title"> {{ ...


3

In order to sort your entries by a related field you need to join the relations and the content table and sort your entries by the column you want. The it doesn't change the behavior of your paginate {# the field ID of your relation field, you can as well grab it via #} {# set field = craft.app.getFields().getFieldByHandle('carCategory').id #} {% set ...


3

I haven't tested it but it should give you a pretty good idea on how to set this up. public function actionTest() { $entryQuery = Entry::find()->section('mySection'); // All the element relations. For example tags and categories. // These have to be Element objects. For example craft\elements\Tag. $myArrayWithTags = [/* ... */]; $...


2

It's probably your second-from-last line: {% endfor % } should be: {% endfor %}


2

If you're just looking to get the author of an entry, it's a simple as: {% for entry in craft.entries.section('blog').limit(1) %} {{ entry.title }} {{ entry.author.firstName }} {{ entry.author.lastName }} {% endfor %} If you're looking to get every user associated with every Users field for an entry, your relatedTo syntax will work: {% for ...


2

You’d need to query for all Matrix Blocks the user is related to using a Matrix Block ElementCriteriaModel. Unfortunatelly this is not possible from the template level by default, you need to either install the plugin Low Blocks, or write some some lines of PHP in a custom plugin. Here’s how you would have to set the params for the Low Blocks function: {% ...


2

I need someone more experienced with Craft to vet this, but it seems that this is working: Supplying an array to relatedTo returns results if it matches one OR the other entry. $projectCriterea = craft()->elements->getCriteria(ElementType::Entry); $projectCriterea->section = 'projects'; $projectCriterea->relatedTo = [$themeEntry, $yearCat]; $...


2

You can pass in a CategoryModel to the relatedTo parameter, so its just a matter of getting the CategoryModel you're after. Something like: {% set category = craft.categories.id(42).first %} {% set entries = craft.entries.section('blog').limit(3).relatedTo(category) %}


2

Something like this should work to combine 'and' and 'or' conditions. 'relatedTo' => [ 'and', ['element' => [124, 99]], ['element' => 105], ['element' => 231], ], And this is the pattern I’m usually using to build the relatedTo param dynamically. I also try to be explicit about the direction (sourceElement / targetElement) and ...


2

You could consider using Categories. Entries Fields(relations) are great, but sometimes not the best organizing solution depending on the project. As you can see from your original post, you're already asking the question "Do I relate this to that or that to this?". I'd stay away from tags as they are hard to manage, prone to error, and always end up a bit ...


2

Try this: {% for category in categories %} <h2>{{ category.title }}</h2>{# spit out the Category #} {% set entries = craft.entries.section('news').relatedTo(category) %} {% for relatedEntry in entries %} {{ relatedEntry.title }} {% endfor %} {% endfor %} See the set entries = ... line. I am retrieving the entries that ...


Only top voted, non community-wiki answers of a minimum length are eligible