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', [$...
Ben Croker's user avatar
  • 7,111
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 ...
Aaron Berkowitz's 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
naboovalley's user avatar
  • 2,834
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 ...
Alec Ritson's user avatar
  • 4,519
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)...
Lindsey D's user avatar
  • 23.8k
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
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, ...
Marcus's 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: ...
crawf's user avatar
  • 3,507
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 ...
naboovalley's user avatar
  • 2,834
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 ...
m9dfukc's 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 ...
Marion Newlevant's user avatar
5 votes

Filter locations by country in Smart Map

What I've done to do something very similar is to add a template variable to my BusinessLogic plugin, that takes the ElementCriteriaModel, prepared with all other parameters e.g. section, limit and ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

How to integrate with Hubspot

I would say you would need to create a custom plugin and use Hubspot webhooks. Within this plugin you would have a controller method that, when you visit the action url, will take a response and ...
Alec Ritson's user avatar
  • 4,519
5 votes

Get all entries attributed with category

Found an answer to this here: https://craftcms.stackexchange.com/a/4132/3890 The important thing to note is when you're using relatedTo you're not passing a slug, you're passing a whole object. So ...
Sebastian Thomas's user avatar
5 votes
Accepted

Is it possible to limit the level at which entries can be selected from within an entries field?

There's already a popular feature request for this: "Entry Type" setting for Entries fields But yes, it's currently also possible to do this by extending the default entries field type and adding ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

SuperTable and related entries

Unfortunately, you can't use Craft's built in relations service for reverse relations lookup with Super Table. This is because there's actually a special case for nested relations, such as a Matrix ...
crawf's user avatar
  • 3,507
5 votes
Accepted

How to query for multiple relations with SuperTable

It's a little hard to follow your example/description, but I'll take a stab at it. The way I interpret your question, you need to pull a single guestList entry that is a) related to the current event,...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

.relatedTo() not returning tags with spaces

In your working example, you’re mapping request.tags to the search param, but in the non-working example, you’re mapping it to the slug param. Those two params are handled in very different ways. ...
Brandon Kelly's user avatar
5 votes
Accepted

Foreign Key column pointing to `craft_relations` table

I might be wrong, but I don't believe you'll be able to do that within the defineRelations() method – Craft needs a record class for the table you want to create a relation with. A workaround is to ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

Combining Multiple Relation

You might have seen Passing Multiple Relation Criteria in the docs {% set entries = craft.entries.section('flightcompany').relatedTo([ 'and', { targetElement: models, field: 'partsModels....
Robin Schambach's user avatar
4 votes
Accepted

Get search results by relationship

Do another call to craft.entries when you loop through the "area" entries and include the section and relatedTo params: {% for entry in searchEntries %} {% set videos = craft.entries.section('...
Mats Mikkel Rummelhoff's user avatar
4 votes

Create new entry from related Entry field

Not sure when this was added, but this is now available in Craft core. The select modal has a button to create a new entry of that type.
Marion Newlevant's user avatar
4 votes
Accepted

How can I use tag slugs in URLs?

The key to this is to use the |kebab filter and the relatedTo params. Listing: {% for tag in relatedTags %} <li><a href="/news/tags/{{ tag.title|kebab }}">{{ tag.title }}</a>&...
darylknight's user avatar
  • 3,290
4 votes
Accepted

Filter locations by country in Smart Map

At the moment, there's no in-plugin way to handle this. However, you can probably do it out-of-plugin, once you've retrieved a larger set of results. So first, you'd get the largest possible results ...
Lindsey D's user avatar
  • 23.8k
4 votes
Accepted

Find entries related to user and display field name

What could work for you is to get the events for each field separatly: {% set firstEvents = craft.entries.section('events').relatedTo({ element: user, field: 'firstEntriesField' }) %} {% set ...
carlcs's user avatar
  • 36.1k
4 votes

List related siblings

As already mentioned in the comments, getSiblings should do exactly what you need. {% for sibling in entry.getSiblings() %} {{ sibling.title }} {% endfor %}
carlcs's user avatar
  • 36.1k
4 votes
Accepted

How to remove relationships between entries

A relationship field expects an array of element IDs. So to remove a user, you'd just get all the current IDs and remove the one you want to delete. // Get the current member IDs $members = $entry-&...
carlcs's user avatar
  • 36.1k
4 votes
Accepted

Get random entries, excluding an array of entry IDs, where a relational field is not empty

That seems to be a bug / limitation of the :notempty: statement, it's probably adding its own ID criteria in the background and the merge doesn't work as expected. I'd suggest you file a bug report. ...
carlcs's user avatar
  • 36.1k
4 votes
Accepted

Prettiest way to loop through nested relations?

Would this work? {% set teams = craft.entries.section('team').relatedTo(studios) %} <h2>All teams in {{entry.title}}</h2> {% for team in teams %} <h3>{{team.title}}</h3> {...
Jamie Pittock's user avatar
4 votes
Accepted

Loop through entries that share current entry category and restrict to Cat Group Id

You can make use of 'not' to exclude items from your parameters. An example of how this would be implemented in the above use case would be as follows {% set categories = craft.categories.slug( 'not ...
Luke Pearce's user avatar
  • 3,843

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