Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
20 votes
Accepted

Users relatedTo multiple categories

Try passing your relatedTo parameters as an array: {% set members = craft.users.group('members').relatedTo([ 'and', { targetElement: services }, { targetElement: locations } ]) %} or a little easier ...
user avatar
  • 35.8k
13 votes
Accepted

How do I pull related entries and assets?

The data in Craft is handled mostly by Models (and arrays of Models). Before pulling actual information, you define your criteria in an ElementCriteriaModel using generic parameters and parameters ...
user avatar
10 votes
Accepted

Using “search” vs. “relatedTo” in a template

To be honest, those two parameters may seem to be slightly similar but provide a totally different functionality. From their documentation on search: search Only fetch entries that match a given ...
user avatar
10 votes
Accepted

How to list entries related to a category + related to a section?

When you call this: craft.entries({section: 'games'}).relatedTo(category).relatedTo(craft.entries.section('reviews')) that second relatedTo param is just overriding the first one. Lucky for you, ...
user avatar
9 votes
Accepted

Craft 3 - Save new Entry with Relations (Categories/Assets)

This should be as simple as setting the relation field (category, asset, etc.) value on the entry as an array of related element IDs before saving. $entry->setFieldValue('categoryFieldHandle', [$...
user avatar
  • 6,156
8 votes
Accepted

Creating comments via a related section

One approach would be to create a comments channel with an entries fieldtype (with sources checked for any channels that you want to allow commenting; limit set to 1) called 'relatedEntry' (or ...
user avatar
8 votes
Accepted

Listing all entries in one section related to entries written by currentUser in another section

I think this is what you mean: {% set sectionBEntries = craft.entries.section('B').authorId(currentUser.id) %} {# 'relatedTo' works both ways, so we can use sectionBEntries as input for ...
user avatar
  • 6,288
8 votes
Accepted

Searching a specific section by four different types of parameters?

One method is to build your params before performing the search. {# set base params #} {% set params = { section: 'portfolio', limit: null } %} {% set relatedParams = ['and'] %} {# status #} ...
user avatar
8 votes
Accepted

How to get entries related to current category only (not children)

Since Apples and Bananas are children of Fruit, all entires that are related to Apples or Bananas are also related to Fruit, which is why you are getting the the results you are getting. If you need ...
user avatar
7 votes
Accepted

Is it possible to create a fieldtype that creates a many to many relationship between two entry sections?

There's still a few kinks to work out, but I developed a plugin that solves this problem using Craft's native Many to Many relationship that already exists. Basically, I've created a field type that ...
user avatar
  • 1,818
7 votes
Accepted

How can I display related entries selected within a Matrix field?

When you relate an element from a Matrix block, it's important to know that the relation is between the Matrix block and the related element; nor the entry (or whatever has the Matrix field) and the ...
user avatar
7 votes

"Eager loading" of relations like Yii's "with" function

This feature has now made it into Craft core as of version 2.6. See this link for documentation on the subject: https://craftcms.com/docs/templating/eager-loading-elements
user avatar
  • 2,824
7 votes
Accepted

Inserting Relationship Into Entries Field - Plugin Not Saving Relation

Figured it out, trying to use the Entry as the source was wrong as the 'stores' relationship field actually belongs to the matrix field. So by setting the matrix field as the source this is now ...
user avatar
  • 4,509
7 votes

Get entries NOT related to categories

If you want to do it in a single query, Marion's answer is probably the best. Otherwise, you could do something like this: {% set idsHavingCategory = craft.entries.section('news').relatedTo(category)...
user avatar
  • 23.6k
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
6 votes
Accepted

How can I follow relations in ElementCriteriaModels from plugin code?

You're very close! Try this: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'concepts'; $criteria->limit = null; $criteria->relatedTo = array( ...
user avatar
  • 1,818
6 votes
Accepted

In a plugin, how do I save an entry with a related entry

Relations fields can store multiple related values (even if you only have one item). Perhaps try to pass an array to the 'quiz' value: 'quiz' => array($data['itemId']) Alternatively, if you are ...
user avatar
  • 13.2k
6 votes

Is it possible to create a fieldtype that creates a many to many relationship between two entry sections?

You can assign ingredients to recipes using the built-in Entries field type. Then, to view which recipes an ingredient is used on, check out the Introvert plugin. It gives you a new field type that ...
user avatar
6 votes

Creating comments via a related section

I've used this method before, works quite nice! The way to do this: Create the fields you need for commenting (name, message, etc.). Be sure to add a relational field to which you can later assign ...
user avatar
  • 6,288
6 votes

"Eager loading" of relations like Yii's "with" function

I've been thinking about ways to achieve this recently, and I've put together a basic proof-of-concept plugin for fetching one level of relations for a collection of elements, with no more than two ...
user avatar
  • 4,361
6 votes
Accepted

How to fix "ambiguous" column error generated when DeleteStaleTemplateCaches is run?

Give this a whirl, Mark: {% for image in craft.assets.relatedTo(tags).order('elements.dateCreated desc') %}
user avatar
  • 66.4k
6 votes
Accepted

Listing Sub Categories of a Category related to an entry

Check out the documentation for craft.categories which should do what you need. In this example, I assume that 'landing pages' are the same as your level 1 categories, in which case you don't need to ...
user avatar
6 votes
Accepted

RelatedTo Query Using Both "and" and "or"

Crisis averted, thanks largely in part to the versitile nature of the relatedTo targetElement processing. The relevation came that I could feed an array of category ids (inclusive) into that element, ...
user avatar
  • 419
6 votes
Accepted

Reverse Related entries with Matrix Field

Have a look at the 'Going Through Matrix' section at http://buildwithcraft.com/docs/relations#the-relatedTo-param. {% set articles = craft.entries.section('articles').relatedTo({ targetElement: ...
user avatar
  • 3,392
6 votes
Accepted

Many db requests with relationship fields

This is how relationships work in Craft at the moment. Each relation equals +1 query. If you don't access your relation on an entry the query isn't executed. So by only fetching title and/or id you ...
user avatar
  • 2,824
5 votes
Accepted

How do I display a list of assets with the entries they are related to?

You probably have to query a new ElementCriteriaModel for each asset in your loop and use the relatedTo parameter to filter the relevant "content" entries. You could use the targetElement property ...
user avatar
  • 35.8k
5 votes
Accepted

Getting entries related to all of supplied categories, if they exist

Completely untested, but try something like: {% set ids = [] %} {% if craft.request.getQuery('sector') %} {% set ids = ids|merge([craft.request.getQuery('sector')]) %} {% endif %} {% if craft....
user avatar
  • 66.4k
5 votes

How to search children with relatedTo param

It appears the preferred way is by using getDescendants() — In fact, the children property is actually an alias for getDescendants(1). (The parameter specifies a maximum distance (i.e. number of ...
user avatar
  • 3,390
5 votes
Accepted

How to use localized categories properly?

To answer my own question see below: As carlcs pointed out correctly the problem actually isn't located in getting the category - which works just well and is the right way to do it - but in how the ...
user avatar
  • 171
5 votes

Get entries NOT related to categories

I got this to work with search: {% set uncategorizedEntries = craft.entries.section('news').search('-category:*') %} Here category is the category field. You are searching everything in news which ...
user avatar

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