Skip to main content
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,631
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
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

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

List only Orders containing specific Products

This is now possible as of Craft Commerce 1.1.1211 using the new hasPurchasables criteria attribute: https://craftcommerce.com/docs/craft-commerce-orders#haspurchasables
Luke Holder's user avatar
  • 6,837
4 votes
Accepted

Show all tags related to a category?

You can use the relatedTo param on craft.tags like you can with any Element inside Craft. All Elements inside Craft can be related to one another. There is a .relatedTo parameter available on all of ...
Luke Pearce's user avatar
  • 3,863
4 votes
Accepted

How/Can does Craft handle ancestor/grandparent relationships?

As usual with Craft you have several options. If you didn't need the index pages, I would suggest a location category, which you organized into 3 levels: states, regions, counties. Selecting a county ...
Marion Newlevant's user avatar
4 votes
Accepted

Delete a relation in frontend form

I believe you're getting that error because you're supplying an array ([]) for the mother field and the only value in that array would be an empty string ("") if they select the There is no mother ...
Stephen Hamilton's user avatar
4 votes
Accepted

Editing entry in front-end

I managed to find a solution, using hidden input fields :) Thank you for all the help :) Edit: Ended up solving this by using tables and saving the username and ID in that. Used a plugin called Super ...
Filip Iversen's user avatar
4 votes
Accepted

Frontend form with relation to an entry

After research with lots of trail & error I found the solution. I just had to add [] to my hidden field to establish the relation: <input type="hidden" name="fields[assetId][]" value="1478" id=...
Marco's user avatar
  • 213
4 votes
Accepted

How do I query two relationship fields where one relationship MUST be empty?

Splitting the criteria up like this works. $criteria->relatedTo = [ 'targetElement' => anElementId, 'field' => 'relationshipOne' ]; $criteria->relationshipTwo = ':empty:'; ...
Benjamin Kohl's user avatar
4 votes
Accepted

Find entries from related section after certain date

You’d first get all the relevant entries of your “trainings” section, or better get just their IDs as that’s way better on performance. Then on another element query you set the relatedTo parameter to ...
carlcs's user avatar
  • 36.3k
4 votes

With a categories field, how do I query only the *selected* categories?

Here's how to isolate only your chosen categories. This works off the premise that if a related category is a parent of one of the other related categories, it is not a category you chose. {% set ...
Evan Warner's user avatar
4 votes
Accepted

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

Syntax for Laravel methods using arrow functions in twig ? You're not doing anything wrong, this approach is simply not supported by Twig. Arrow functions in Twig can only be used with built-in ...
MoritzLost's user avatar
  • 11.6k
3 votes
Accepted

How do I include record relationships in validation?

Yii/Active Record won't do this by default. You'd need to manually add an exists validation rule to the typeId attribute to get it to behave like you're looking for.
Brad Bell's user avatar
  • 67.5k
3 votes

Relating a Category Entry to a Categories Field Type

Turns out I was overthinking it, I was able to connect it with the following piece of code. {% set levelOne = category.topLevelConnection %} {% for subCat in craft.categories.group('levelTwo')....
conormcafee's user avatar
3 votes
Accepted

Using the Search Filter with Entry Fields

{% set range = entry.model %} {% set categories = craft.categories.slug('not preferential') %} {% set entries = craft.entries.section('offers').search('title:' ~ range).relatedTo(categories) %} If ...
mcclaskiem's user avatar
3 votes
Accepted

Sorting across blocks (to get prev/next links)

You can get all of the works entries related to your contents Matrix field like so: {% set works = craft.entries({ relatedTo: { sourceElement: issue, field: 'contents.works' } }) %} They will ...
Brandon Kelly's user avatar
3 votes

List only Orders containing specific Products

Looks like what I'm trying to do isn't possible (yet). There is a suggestion for a solution (not created by me) already on the Craft Feedback site. So if you think this would be a useful thing to ...
Steve Holland's user avatar
3 votes
Accepted

Get disabled related entry's data within plugin

Came across this same situation myself. Was able to resolve with: $entry->entriesFieldWithDisabledEntry->status(null)->first()
Chase Giunta's user avatar
3 votes

Editing entry in front-end

Looks like you want to implement a feature to vote for entries. Have a look at these plugins, they make this really easy for you. https://www.doublesecretagency.com/plugins/upvote https://github.com/...
carlcs's user avatar
  • 36.3k
3 votes

How, in a plugin, can I check that a User's "Favourite Entries" field contains a specific Entry?

You can avoid looping by getting an array of the IDs of the favourite entries, then check to see if the specific Entry ID is in the array. $favouriteEntryIds = $user->favouriteEntriesField->ids(...
Steve Rowling'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

Listing all entries which belong to an entry

I had a similar problem to yours, and here's what I ran into. It may be that you're using entry for your main info on the retailer entry page? Because {{ for entry in relatedProducts }} would return ...
SamC's user avatar
  • 482
3 votes
Accepted

Why isn't relatedTo not working (anymore)?

You are so close! All you need to do is to remove the ’.slug’, because that is what the param is expecting.
carlcs's user avatar
  • 36.3k
3 votes

How can I work with related entries within a Matrix in a plug in?

It’s an entries field, so $skill->skillName returns just the ElementCriteriaModel prepared to query for one or more elements matching the criteria. You need to do something like this: $entry = $...
carlcs's user avatar
  • 36.3k
3 votes
Accepted

How can I work with related entries within a Matrix in a plug in?

Your skillName is a object of type ElementCriteriaModel so it is just a query. In order to fetch the element you need to do $skill->skillName->first() You should read the docs about how to ...
Robin Schambach's user avatar
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
Accepted

Front End Search Related Entries

Make sure that you checked Use this field’s values as search keywords under Settings → Fields → Your field With that in place, run ./craft resave/entries --update-search-index (docs) and your entries ...
Oli's user avatar
  • 7,495

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