8 votes
Accepted

Get related posts excluding current post in Craft 3

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() ...
user avatar
  • 2,707
7 votes
Accepted

Filtering multiple categories with URL queries

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 ...
user avatar
4 votes
Accepted

How to display number of entries for a tag and not display if no entries?

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....
user avatar
4 votes
Accepted

Fetch target entries that have been linked through an Entries field

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 ...
user avatar
  • 35.8k
4 votes
Accepted

Edit entry, on the front end, with the entries field

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 ...
user avatar
4 votes
Accepted

How to use Sprig's "Load More" Recipe After Update

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). ...
user avatar
  • 7,095
3 votes

Most related articles

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 ...
user avatar
  • 7,228
3 votes
Accepted

Need a little help with basic understanding of how categories work

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?...
user avatar
  • 564
3 votes

Need a little help with basic understanding of how categories work

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 ...
user avatar
  • 3,195
3 votes
Accepted

How can I filter selected/related entries by their categories?

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 #} {...
user avatar
  • 2,563
3 votes
Accepted

Selecting a subset of entries, based on a relatedTo call

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 = { ...
user avatar
3 votes
Accepted

Filter results by users contained in a Users Relations RelatedTo field

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 ...
user avatar
3 votes
Accepted

RelatedTo does not seem to work!

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.
user avatar
3 votes

Multiple category relationship criteria (or)and(or)

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 ...
user avatar
3 votes
Accepted

cannot get relatedTo by slug only ID

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 ($...
user avatar
3 votes
Accepted

Merging Multiple Relations as params

Something like this should work: {% set myStation = getCookie('myStation') %} {% set relatedStation = craft.entries.id(myStation) %} {% set params = { section: 'blog', limit: 6, offset: 1, relatedTo:...
user avatar
  • 66.4k
3 votes
Accepted

Craft 3 Element API: Query the "parent" entry of a "child" entry with Matrix blocks connected by an 'Entries' field

$page = Entry::find() ->relatedTo([ 'targetElement' => $article, 'field' => 'articleSections.articles' ]) ->section('page')->all(); Please read ...
user avatar
3 votes

Reverse relationships with entries and users

{% set people = craft.users.group('family').ids() %} {% set relatedPlaces = craft.entries.section('locations').relatedTo({ targetElement: person, field: 'relatedPeople' }) %} Grab all the ids ...
user avatar
3 votes

Returning entries that contain both categories

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 ...
user avatar
3 votes

Returning entries that contain both categories

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

Commerce discount promotions as a fieldtype?

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 ...
user avatar
3 votes
Accepted

Showing a list of selected/related categories to entry

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 = ...
user avatar
3 votes

Order entries by a category field alphabetically then paginate

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 ...
user avatar
3 votes

Get entries based on multiple fields in controller

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 ...
user avatar
3 votes
Accepted

Order by reverse relationship count

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() ...
user avatar
  • 7,095
3 votes
Accepted

Change link to entry if it's related to another entry

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 ...
user avatar
3 votes

Change link to entry if it's related to another entry

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(...
user avatar
  • 6,442
2 votes
Accepted

Error in case of nested for loops

It's probably your second-from-last line: {% endfor % } should be: {% endfor %}
user avatar
  • 4,361
2 votes

Get User Object for a given Entry

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....
user avatar
  • 66.4k
2 votes
Accepted

Show on user profile page the commissions he is connected to

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 ...
user avatar
  • 35.8k

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