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() ...
a-am's user avatar
  • 2,857
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 ...
Mats Mikkel Rummelhoff's 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....
Mats Mikkel Rummelhoff's 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 ...
carlcs's user avatar
  • 36.2k
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 ...
Robin Schambach's user avatar
4 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 ...
Robin Schambach's 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). ...
Oli's user avatar
  • 7,410
4 votes
Accepted

Set parent-child relationship in PHP

Just to clear up a potential source of confusion: For Craft, the only 'real' parent-child relationships are the hierarchies in Structures (a type of Section). Entry fields and by extension relations (...
MoritzLost's user avatar
  • 10.7k
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 ...
André Elvan's user avatar
  • 7,268
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?...
Ian's 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 ...
darylknight's user avatar
  • 3,290
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 #} {...
Alex Roper's user avatar
  • 2,622
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 = { ...
Krzysztof Boduch's 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 ...
Mats Mikkel Rummelhoff's 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.
user5400's 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 ...
Marion Newlevant's 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 ($...
Robin Schambach's 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:...
Brad Bell's user avatar
  • 67.2k
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 ...
Robin Schambach's 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 ...
Robin Schambach's 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 ...
Robin Schambach's 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() %}
Mats Mikkel Rummelhoff's 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 ...
Brandon Kelly's 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 = ...
Jalen Davenport's user avatar
  • 3,011
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 ...
tim-pixeldeluxe's 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() ...
Oli's user avatar
  • 7,410
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 ...
Evan Warner's 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(...
RitterKnight's user avatar
  • 6,582
2 votes
Accepted

Error in case of nested for loops

It's probably your second-from-last line: {% endfor % } should be: {% endfor %}
Mike Pepper's user avatar
  • 4,391
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....
Brad Bell's user avatar
  • 67.2k

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