8

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() %}


7

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, ...


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', ...


4

The problem is that you are doing .relatedTo(category) but the variable you are passing is called relatedTo, not category. Just like with limit, based on your code, it should be .relatedTo(relatedTo). Also, it should be part of the same hash as your limit. However, you can't pass a category object to a component and will need to pass its id instead (docs). ...


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

There is a in operator to check if a variable is in a string or an array. Your entries field actually contains an object of type ElementQuery instead of an array of elements. All you need to do is to check if the current entry is in the list of selected entries {% set selectedEntries = entry.planItems.ids() %} {% set entries = craft.entries.section('...


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 = [/* ... */]; $...


3

If you don't have tons and tons of creators, you could do that by querying creators and re-arranging the results: {# Get all creators and eager-load the products #} {% set creators = craft.entries() .section('creators') .with('products') .all() %} {# Declare an array to store all related product ID #} {% set products = [] %} {# Build an array ...


3

So if I'm understanding corrently, your Review entries contain multiple entries fields, one of which stores News entries related to the product review? If that's correct, then let's assume that field is called relatedNews. You can utilize advanced relations to find what you're looking for. So for an individual News entry you could do the following: {% set ...


3

Lots of pieces to this question. Since you need to check each field, something like: {% set whichField = null %} {% for checkField in ['field1','field2','field3','field4'] %} {% if attr(relatedReview,checkField) is not empty %} {% set whichField = checkField %} {%endif%} {% endfor %} {{ relatedReview.url ~ '#' ~ whichField }}


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: {% ...


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