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', [$...
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 ...
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
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 ...
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)...
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 ...
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, ...
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: ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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. ...
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 ...
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....
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('...
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.
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>&...
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 ...
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 ...
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 %}
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-&...
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.
...
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>
{...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
relations × 325entries × 64
categories × 59
matrix × 40
plugin-development × 33
relatedto × 31
templating × 22
entries-field × 20
craft3 × 19
search × 18
assets × 14
users × 14
elementcriteriamodel × 13
structure × 12
tags × 11
query × 11
entry × 9
category × 9
ordering × 9
plugin-craftcommerce × 8
database × 8
plugin-elementapi × 8
performance × 7
elementquery × 6
plugins × 5