Skip to main content

Questions tagged [relations]

Questions around Craft's relational field types.

Filter by
Sorted by
Tagged with
9 votes
1 answer
7k views

Users relatedTo multiple categories

Trying to get users in a members group related to multiple categories. {% set services = craft.categories.group('services').slug('service-1, service-2') %} {% set locations = craft.categories.group('...
Sean Delaney's user avatar
9 votes
1 answer
3k views

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

Sorry if this has been asked before. I've searched for other relatedTo questions but could not find anything similar. I have 2 sections (each having 1 entry type): games reviews Games are linked ...
user avatar
8 votes
2 answers
707 views

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

So, for example: Say I have two sections: - Recipes - Ingredients Recipes can have many ingredients. Ingredients can have many recipes. If I'm editing a Recipe, I can edit the ingredients. This ...
Peter Tell's user avatar
  • 1,828
8 votes
1 answer
918 views

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

I have problems to understand the difference between search and relatedTo. From my understanding, this is a fulltext search on all fields. {% set entries = craft.entries.search(test).order('score') %}...
MISC's user avatar
  • 1,019
8 votes
1 answer
990 views

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

I need to get entries which are only in current category, not child categories. So if I have a category structured like this: Food ----Fruit -------Apples -------Bananas I've got a categories ...
Mark Gifford's user avatar
7 votes
3 answers
1k views

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

Is there any way to "eagerly load" Craft element relations a la Yii's ActiveRelation "with" function? http://www.yiiframework.com/doc/api/1.1/CActiveRecord#with-detail This seems like a very handy ...
Chris Lewis's user avatar
7 votes
2 answers
904 views

How to use localized categories properly?

I've setup a localized site (de/en) with custom routing for my categories (which are marked as translatable). I use a tag route for my categories which points to the same template as my structure. ...
m9dfukc's user avatar
  • 171
7 votes
1 answer
1k views

Eager loading of reverse related elements?

Is it possible to load reverse related elements? I mean if the elements I’m querying are the target elements? Example: I’m setting up a criteria model to get some categories and I also want to load ...
carlcs's user avatar
  • 36.3k
6 votes
3 answers
2k views

How can I display the related posts (by category) at the bottom of each post?

I'm converting a WordPress blog to Craft - first time playing with categories in Craft, so how can I display the related posts (by category) at the bottom of each post?
lucassoares's user avatar
6 votes
1 answer
2k views

Searching a specific section by four different types of parameters?

I'll do my best to explain this. I have a Portfolio of companies, which I have as a structure, that I need to be able to filter/search through by using up to four different parameters. Keyword - a ...
zackspear's user avatar
  • 545
6 votes
1 answer
545 views

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

Consider an employee listing... I have 2 sections: 1 for companies, 1 for personnel. In the personnel section, there is a matrix field within which one field is a relational one to relate the staff ...
V. Arora's user avatar
6 votes
1 answer
1k views

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

Following up on: Deleting Stale Template Caches Failed Here's what I'm assuming is the tag throwing the error in the DeleteStaleTemplateCaches task. How would I disambiguate this? I'm not sure ...
Mark J. Reeves's user avatar
6 votes
1 answer
1k views

How can I follow relations in ElementCriteriaModels from plugin code?

In Twig code you can use craft->entries->relatedTo to do all sorts of neat things. From plugin code I am not sure how this works? $c = craft()->elements->getCriteria(ElementType::Entry)...
gromgull's user avatar
  • 631
6 votes
1 answer
851 views

How can I reduce the number of queries when including a related item within a for loop

I've created an alphabetical index of entries in my template. The code I'm using is in the gist below: https://gist.github.com/jamiepittock/2635516e95317e5c6ed6 It results in a layout like the one ...
Jamie Pittock's user avatar
5 votes
1 answer
1k views

Filtering multiple categories with URL queries

I am trying to filter using categories. I have to filters type and size, in the url. /products.html?size=medium,large&type=polo-shirt,jeans In my template, I am doing the following {% set sizes ...
Hassan Mahmood's user avatar
5 votes
2 answers
520 views

List only Orders containing specific Products

Each Product has a Merchant field which is standard Craft User. What I'd like to do is list out all Orders which contain one or more Products relating to the currentUser. I was hoping I'd be able to ...
Steve Holland's user avatar
5 votes
3 answers
1k views

Get image from related entries asset field

I have a "single" entry which has a "entries" field. The related entries have a image field of the "Assets" type. In the template, i want to display the image from each of the related entries. I ...
John's user avatar
  • 974
5 votes
1 answer
293 views

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

I'm trying to pull a set of 3 random entries from a Section. The entries need to have a certain Assets field not empty, and I also have to exclude entries already pulled (these entries are stored in ...
Mats Mikkel Rummelhoff's user avatar
5 votes
1 answer
1k views

How can I create an ElementCriteriaModel with 'and' and 'or'

In my template, I want to: Get all the entries in the section 'uploads'; Get all the assets that are related to one of the upload entries, and have a relation to all the tags I provide. So, that ...
Paul's user avatar
  • 6,338
5 votes
1 answer
633 views

Is there a good way to eager load Craft's Element Relations through a plugin?

For a specific entry in a specific section I would like to eager load all associated data from the related elements. Say that I know the related elements are from the following field types: An ...
Peter Tell's user avatar
  • 1,828
4 votes
3 answers
1k views

Creating comments via a related section

I've been reading on here about suggestions on implementing commenting systems. I'm aware of disqus integration but I want/need built in commenting. I have seen it mentioned a couple of times that ...
shorn's user avatar
  • 438
4 votes
2 answers
1k views

Inserting Relationship Into Entries Field - Plugin Not Saving Relation

I was hoping someone could shed some light on this issue I'm having. I'm trying to update a relationship field through a plugin, without much success, the entry looks like this: I saw a post on here ...
Alec Ritson's user avatar
  • 4,539
4 votes
1 answer
3k views

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

i simply can not get my "job" done. I try to create a plugin for Craft, a simple one. It reads some date and should create new entries out of this data. First i stuck on a stupid save-mistake, someone ...
Thomas's user avatar
  • 85
4 votes
2 answers
709 views

Filter locations by country in Smart Map

I'm using Smart Map to find locations ordered by distance. But I can only set 3 parameters: target, range and units. However, when my range goes beyond the country borders I obviously get results in ...
Tom De Smet's user avatar
  • 1,456
4 votes
1 answer
442 views

Many db requests with relationship fields

I have a page with a lot of entries fields. They are awesome but that leads also to a LOT of db requests. Aside from caching are there some best practices to lower db requests on entries fields? For ...
KSPR's user avatar
  • 3,786
4 votes
1 answer
614 views

Reverse Related entries with Matrix Field

I have a site with channels ‘articles’ and ‘tutorials.’ An article entry has a matrix field which contains an entry field to link to a tutorial. Every tutorial is linked from within at least one ...
David Schafer's user avatar
4 votes
1 answer
577 views

RelatedTo Query Using Both "and" and "or"

I'm attempting to build a filter that queries entries of a section based on two criteria: A selection of one or more categories (inclusive), and a single related entry (exclusive). Specifically, the ...
Marcus's user avatar
  • 419
4 votes
1 answer
2k views

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

In my plugin, how do I save a relationship when creating a new entry? The example below works for all but the "quiz" relationship. $triviaSet = craft()->entries->getEntryById($data['itemId']); $...
Dan Eichholz's user avatar
4 votes
2 answers
952 views

Craft 4: using the new laravel collections and related methods (in a performant way)

I am working on a website for a radio that publishes its programs as podcasts with episodes. Each of the 30 or so programs potentially has a lot of episodes. Each entry in the episodes channel has a ...
Jérôme Coupé's user avatar
4 votes
2 answers
202 views

How do I include record relationships in validation?

Say I have a record defining a foreign key relationship which is set to required, such as: public function defineRelations() { return [ 'type' => [ static::BELONGS_TO, ...
Jamie Pittock's user avatar
4 votes
1 answer
463 views

Foreign Key column pointing to `craft_relations` table

I am writing a plugin with a Record. I want the primary key of the table to also be a foreign key relating to the craft_relations table. Ordinarily, I would add something like this... public function ...
Lindsey D's user avatar
  • 24k
4 votes
2 answers
1k views

How do set multiple entry relations using checkboxes on a front end form?

I want to set up a form on the front-end of a site to allow logged in users to choose items from a list which represent entries in a channel using checkboxes, then save those entries as relations to ...
Steve Rowling's user avatar
4 votes
1 answer
289 views

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

I'm trying to get all entries that related to one .. three categories, as determined whether their id appears in the query string: {% set sector = craft.categories.id(craft.request.getQuery('sector'))...
Daniel's user avatar
  • 343
4 votes
2 answers
1k views

How to query with relatedTo of a Relationship Field in a Matrix Field block

I have structures called Instrument and Person. On the person structure, I have a Matrix field called Roles. Roles has a block called Musician. And That block type has a Relationship Field called ...
Bill Columbia's user avatar
4 votes
1 answer
2k views

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

Given a list of assets, for each asset, how do I display a list of entries that relate to this asset? So, for instance, if asset A is selected as asset in entries 1 and 2, I want a list to be ...
Paul's user avatar
  • 6,338
4 votes
2 answers
99 views

How to model a multiple relation situation?

I have a site that has these different types of content: Candidates Questions Answers So there's a list of questions, that have answers that are related to a candidate. I imagine the models look ...
Dennis Eusebio's user avatar
4 votes
1 answer
172 views

Display related entries across different entry types using tags in different tag groups?

I'm working on a site that has two different entry types (e.g., "blog" and "photos"), and each type has it's own tag group. When viewing a particular entry, I'd like to display related entries found ...
Jason Morehead's user avatar
4 votes
1 answer
1k views

What's the best way to paginate through entries from an entries field type?

I have an entries field type associated with a structure section page. This displays the entries on this page in a particular order. When you click an entry link ti will take you to a detailed page ...
Stuart McCoy's user avatar
4 votes
1 answer
2k views

Checking if current entry exists in an entry custom field in a different entry

I thought this would be simple but I am having no luck with it. I have a channel called "Posts" and for each Post there an "entry" custom field which lists all "Projects" (a structure). The user can ...
Anna's user avatar
  • 127
4 votes
1 answer
322 views

Limit search results for users by tag (and limit to 5 tags)

I've read as many forum posts and documentation but am a little stumped over a specific use-case. Currently the search page grabs the url segment, loops through a verified user group, and uses the ....
Rob's user avatar
  • 865
4 votes
2 answers
380 views

Is it possible to order related entries by relevance of tags?

I have a set of entries which each have 0 or more tags (no limit). For each entry, I am aiming to display 3 'related entries', related by the tags field. e.g. If entry 1 is tagged with 'apples', '...
Mike Vaux's user avatar
  • 480
4 votes
1 answer
237 views

Filter entries on a page based on related entry

This is blowing my mind! I have a Section with handle practiceAreas. I have a Section with handle barristers with approximately 100 entries. barristers has an Entries fieldType called specialisms ...
Martin's user avatar
  • 1,203
4 votes
1 answer
964 views

Related to entries in categories in a plugin

Ok I can do this in twig but after searching high and low I can't find a good example of how to do it in a plugin. I have a user profile field called interests which holds an array of what they are ...
Lettie's user avatar
  • 2,033
4 votes
1 answer
393 views

Filtering on related entries (with an array query param)

I'm getting myself into a potential pickle here and wanting to know the best way to get apply filters on the current scenario: I have a central business section which is getting populated via an API (...
Darren Wood's user avatar
3 votes
4 answers
4k views

Get entries NOT related to categories

is it possible to grab all the entries in a section that arent related to a category? So something like craft.entries.section('news').relatedTo('not',category) Doesnt seem to work but is there some ...
Keith Mancuso's user avatar
3 votes
3 answers
753 views

Get disabled related entry's data within plugin

I'm looping through products ($rawProduct) within Craft Commerce, within a plugin, and returning products which fulfill a certain criteria. I'm then returning a $products array of objects with the ...
Clive Portman's user avatar
3 votes
1 answer
129 views

.relatedTo() not returning tags with spaces

I think I'm seeing a bug while using advanced search with relatedTo(). If I use a simple search, like this: {% set param = { order: 'RAND()', limit: null, search: "#{request.tags}" } %} It ...
Rob's user avatar
  • 865
3 votes
1 answer
604 views

Get all entries attributed with category

Maybe I'm going mad here, but I can't figure out how to get all entries which are in a certain category. Following the syntax laid out here: http://buildwithcraft.com/help/entries-related-to-category ...
Sebastian Thomas's user avatar
3 votes
2 answers
784 views

Two-way relations

I have a section 'people' which has several related entries such as 'sibling', 'spouse', 'offspring', etc. When creating a new 'person' the user has to set up these relationships. I understand that's ...
hamishtaplin's user avatar
3 votes
1 answer
319 views

Find entries from related section after certain date

Does anyone know how to achieve the following in Twig with Craft 3: My sections locations and trainings. I want to find all the entries from locations which has a relation to trainings and a ...
jschmitz's user avatar

1
2 3 4 5
7