Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
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 ...
user avatar
  • 66.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 ...
user avatar
  • 6,156
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 ...
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();
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('...
user avatar
4 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(); $...
user avatar
  • 621
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, ...
user avatar
  • 6,156
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 ...
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, '...
user avatar
3 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 ...
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(...
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() ->...
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}}, '...
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 ...
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 ...
user avatar
  • 6,156
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()...
user avatar
  • 7,105
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 ...
user avatar
  • 3,829
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....
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....
user avatar
  • 6,156
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 ...
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 ...
user avatar
  • 6,070
3 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 ...
user avatar
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 ...
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 ...
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 ...
user avatar
  • 917
2 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 ...
user avatar
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 ...
user avatar
2 votes
Accepted

How to get the unverifiedEmail value of a user after a change email request?

You need to select the value {% set user = craft.users .id(currentUser.id) .addSelect(['users.unverifiedEmail']) .one() %} {{ user.unverifiedEmail }}
user avatar
2 votes
Accepted

Date Limit on Element Query

Have you tried .renewalDate('and', ">= #{dateStart}", "< #{dateEnd}") The difference is the double quotes. I don't think that single quotes support interpolation. However, I'm not sure in ...
user avatar
  • 166
2 votes
Accepted

Set OrderBy on Grouped Query Involving Smart Map Field

Okay, I've been able to solve this and figured I'd share what I discovered. The key for each group is the value of the state so really what I was after here was a key sort and just didn't realize it. ...
user avatar
  • 1,022

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