6 votes
Accepted

Impossible to invoke a method ("limit") on an array... but it's not an array?

I'm not calling .all(), so this is still an object, right? Nope. When you execute the for loop, it will explicitly run the database query and return the array of results. You're probably getting a ...
Brad Bell's user avatar
  • 67.4k
6 votes
Accepted

Element query - using "search" vs using field handle as query method

The search parameter gives you an entire range of search functionality, as explained in the docs at https://craftcms.com/docs/3.x/searching.html Using a field handle as a parameter essentially ...
Ben Croker's user avatar
  • 7,266
5 votes
Accepted

.first() vs. .one() in craft 3

.all() returns a collection of elements, which you can loop through with a for loop. .one() returns a single element, so you cannot use a for loop to access it. In this case you should just store ...
Jeremy Gimbel's user avatar
5 votes
Accepted

Craft 3 plugin - how to update entry with field of Entries type

Ok this was easy. Tried it at first but didn't work and was successful after another go. All it needed was a array of Entry IDs. Working code: $entry = Entry::find()->id($entryId)->one(); $...
biko's user avatar
  • 670
5 votes

Element query for specific fields

@MoritzLost's answer should be the accepted one. But, as an alternative approach you could also use the |map Twig filter to "prune" the entry objects returned from a normal element query: To ...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

How to query entries by field values

There is a related to parameter you can use Entry::find() ->section($section) ->relatedTo(['targetElement' => $image->id, 'field' => 'galleryImages') ->all();
Robin Schambach's user avatar
4 votes
Accepted

Element query: find entries where a field's value is greater than, or less than another field's value?

I agree that any solution where you run a query and then conditionally render the result isn't very efficient. You can do this in SQL by using the where() condition. craft.entries .section('...
Matt Banner's user avatar
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

Advanced Element Queries - example uses

These are simply low level operations you can do to more specifically filter the elements that should be returned. It is generally more common to use them in plugins/modules than in Twig templates, ...
Ben Croker's user avatar
  • 7,266
3 votes
Accepted

How to support chained Element queries with Twig tags

You need to return the instance in every single set call. As you noticed correctly the last line in every function that changes properties of your query is return $this which allows you to chain ...
Robin Schambach's user avatar
3 votes
Accepted

relatedTo() only on one field?

Haha. I figured it out. Leaving the answer here in case anyone else runs into the same problem: $product = Entry::find()->section('products')->relatedTo([ 'element' => $component, '...
Tony DeStefano's user avatar
3 votes
Accepted

Difference between chained and batch element queries?

general The first way creates an object of type EntryQuery, you then call the 2 functions section and limit In PHP you would do the following: $query = Entry::find()->section('news')->limit(...
Robin Schambach's user avatar
3 votes

Element Query matrixfields where

When joining the tables you get more results like you mentioned in Edit2. First pick distinct ids, to use later on in the main query. I came up with this: $timeSlotIds = Entry::find() ->...
Jeroen's user avatar
  • 308
3 votes

Element Query matrixfields where

You need to join the matrixblocks and the specific content table $query->join('{{%matrixblocks}}, 'matrixblocks.ownerId = elements.id); $query->join('{{%matrixcontent_your_table_name}}, '...
Robin Schambach's user avatar
3 votes
Accepted

Craft query Matrix blocks in twig

Yes you can. craft.entries returns an object of type EntryQuery There is a function matrixBlocks in the CraftVariable that returns an object of type MatrixBlockQuery Both of them have nearly the ...
Robin Schambach's user avatar
3 votes
Accepted

How to create an element query in PHP with custom field names in variables?

You can use the Craft::configure() for that.. imagine your array looks like $criteria = [ 'age' => ['and', '>=30', '<=50'], 'enabled' => false 'status' => null, ] You can ...
Robin Schambach's user avatar
3 votes

Craft 3 plugin - how to update entry with field of Entries type

As per the docs at https://craftcms.com/docs/3.x/extend/element-types.html#saving-custom-field-values When saving values on a custom field, always use the setFieldValue() or setFieldValues() methods ...
Ben Croker's user avatar
  • 7,266
3 votes
Accepted

Combine AND and OR in a ElementQuery

If I understand properly, you should be able to do this: $entries = \craft\elements\Entry::find() ->section('yourSection') ->category(['and', 695]) ->relatedTo([698,698])->all()...
Oli's user avatar
  • 7,485
3 votes
Accepted

Dynamically populating select input fields using jquery $.load and twig template

Your code looks fine and worked for me when testing locally. However it could be failing due to the fact that you're not url-encoding the parameter before you send the request. If the values contain ...
James Smith's user avatar
  • 5,004
3 votes
Accepted

How do you eager load or join reverse relationships for entries?

Ok so here's how I ended up doing this. Essentially querying everything and adding a sourceId and targetId property to each result. In order to get this query to work though, we need to add a behavior ...
Myles Hyson's user avatar
3 votes
Accepted

Is there a way to query for a list of values belonging to a specific field in a queryset?

Yep! It's not super well documented, but there's a little information about it down in the Advanced Element Queries section of the docs. It would look something like: {% set titleList = craft....
brianjhanson's user avatar
3 votes
Accepted

Efficient way to get all related entries assigned to matrix fields across a set of entries

If you are trying to get all people entries related to the event entry itself (what you call event "e") then I would suggest going through matrix as follows. {% set people = craft.entries....
Ben Croker's user avatar
  • 7,266
3 votes

Cannot pass where Params to query with Twig variable

You may be overthinking this! It's pretty rare that you'll need to reach directly for the ActiveQuery where, orWhere, and andWhere methods—unless I've misunderstood what kind of results you're going ...
August Miller's user avatar
3 votes
Accepted

Element query for specific fields

Yes, you can change both the columns to be selected and execute queries in several ways to get the results in different formats. In this case, you want to use select() to select the title column and ...
MoritzLost's user avatar
  • 11.1k
3 votes
Accepted

Collections groupBy

Whether you use groupBy (for Collections, Craft 4+ only) or Craft's group filter (for arrays), you'll get back an array where the keys are the thing you grouped by, and the values are nested arrays ...
James Smith's user avatar
  • 5,004
3 votes
Accepted

Excluding entries in an entry query with an array of IDs (PHP)

The implode function will turn the array of IDs into a comma-separated string. Then you're putting that into an array with 'not' as the first element, so the parameter you're passing to id() looks ...
MoritzLost's user avatar
  • 11.1k
2 votes

How can I filter a for loop based on the value of a field?

You don't filter a loop but you can attach certain criteria in your ElementQuery. craft.entries returns you an object of type EntryQuery (link to docs). You can search for specific entries based on ...
Robin Schambach's user avatar
2 votes

How do you limit the number of items that show up in a navigation?

If this is a Craft 2 project, unfortunately there's not a super clean way to do that. You'd have to get the last top-level item in the nav you want to show, and add a bunch of conditions in the code ...
Brandon Kelly's user avatar
2 votes
Accepted

Craft 3 entries "before" not working

This is down to lacking documentation. Before/after are supposed to take "DateTime|string" as a parameter in the ElementQuery. The format is not documented. Use "Y-m-d H:i:s" {% set start = "2 ...
JamesNZ's user avatar
  • 937
2 votes
Accepted

Joining sections (Channel) to query based on a related item attribute

Not sure about doing this in a single query. Referencing this doc page section, https://docs.craftcms.com/v3/relations.html#templating, you can make use of relatedTo() by passing in IDs. Assuming you ...
Sam Hernandez's user avatar

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