4

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 deprecation error logged using that syntax as well since Craft wants you to use {% for album in albums.all() %} See here in the docs: https://docs.craftcms....


3

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 another function after the call. Simple as that. Always return the query until you execute it with all() or another function like that is enough


3

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(10); The second creates an array section => 'news', 'limit' => 10 you then call Yii2::configure($query, $array) with your EntryQuery which does: public ...


3

There is a related to parameter you can use Entry::find() ->section($section) ->relatedTo(['targetElement' => $image->id, 'field' => 'galleryImages') ->all();


3

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() ->section('activities') ->innerJoin('matrixblocks', 'matrixblocks.ownerId = elements.id AND (SELECT enabled FROM elements WHERE matrixblocks.id = elements.id) = ...


3

You need to join the matrixblocks and the specific content table $query->join('{{%matrixblocks}}, 'matrixblocks.ownerId = elements.id); $query->join('{{%matrixcontent_your_table_name}}, 'matrixcontent_your_table_name.elementId = matrixblocks.id); $query->andWhere([ '>=', 'matrixcontent_your_table_name.name-of-the-column', new ...


3

You can use the Craft::configure() for that.. imagine your array looks like $criteria = [ 'age' => ['and', '>=30', '<=50'], 'enabled' => false 'status' => null, ] You can do $query = User::find(); Craft::configure($query, $criteria); $users = $query->all(); Or you can just use the parameter based syntax instead of the ...


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 the entry like: {% set entry = craft.entries.section('thing').one() %} and then access {{ entry.title }} afterwards. (Also keep in mind that entry is ...


3

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(); Assuming category is the name of your relational field, you can use that to pass the and condition (though with one element only it wouldn't be needed) then ...


3

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 spaces or other special characters, for example, this would make it fail. To fix that issue, try using encodeURI() like this: $("#second-choice").load("{{ ...


2

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 different criterias by calling magic methods or function in your Query object. For example: section('docs') will return only entries in the section docs. You can ...


2

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 to make sure the entry you're outputting isn't after that 5th entry. {# get the 5th top-level page #} {% set fifthElement = craft.entries() .section('...


2

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 months ago"|date("Y-m-d H:i:s") %} Behind the scenes in building the query the CMS was replacing any dates I sent through with "2017-12-31 11:00:00" (no idea where ...


2

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 same functionality since they extend the ElementQuery {% set allMatrixBlocks = craft.matrixBlocks.all() %}


2

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 know the ID of the category already and that ID is 1: {% set showIds = craft.entries.section('shows').relatedTo(1).ids() %} {% set entries = craft.entries....


2

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


2

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 that case why the version without the 'and' works. OK, have just tried it myself... I created a section called test and added a single date field called ...


2

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. So I found Michael Rog's "SuperSort" plugin which has a twig filter for running a ksort and that addressed it perfectly for me making a small adjustment to use ...


2

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, 'field' => 'productComponents' ])->one(); You can pass an object/array to the relatedTo() method as described here: Relations | Craft 3 Documentation


2

I figured this while posting so I thought I'd just add it here anyway for others to reference. To retrieve a Soft Deleted Element using the getElementById method you can pass the fourth argument to the getElementById method and define $criteria setting trashed to true, which will include Soft Deleted items in your results: $element = Craft::$app->...


2

Craft does not allow for this (as of Craft 3.1.34). When saving selected categories to a Category relationship field, it automatically assigns the parent categories and does nothing to designate which categories were actually selected/clicked/chosen.


2

Was missing the square brackets {% set groupedEvents = craft.entries(params).eventDate(['and', '>= ' ~ startdate, '<= ' ~ enddate]) | group('eventDate|date("Y-m-d")') %}


2

First, I might over complicate things. Maybe there is a simple solution by using: https://docs.craftcms.com/v3/relations.html#passing-multiple-relation-criteria But my project has a complex query built in. So I did a custom db query class. And here is one of them might working for you. If you know how to create a twig function in craft 3 in modules, the ...


2

You can use the revisionOf parameter (docs). {% set revisions = craft.entries.revisionOf(element.id).all|default(1) %}


2

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(); $entry->textField = 20; // works $entry->entriesField = [$relatedEntryId1, $relatedEntryId2]; // works Craft::$app->elements->saveElement($entry);


1

This tweet shows a way to adjust the elements per page dynamically with a query string param -> https://twitter.com/JakeDohm/status/1157305040278839296?s=20 so you could just request 6 or 9 items to get the same result.


1

You would need to join the array into a string, like so: // Begin defining the response data for this entry $data = [ 'title' => $entry->title, 'slug' => $entry->url, 'status' => $entry->status, 'textArea' => implode(' ',$bodyBlocks) ];


1

If your craft_searchindex table (assume your table prefix is "craft_") isn't too big (I think 100k rows is big), and your field in "relatedMerchant" is the 'title' field. You can do it in one step. $locationsRelatedToCategories = Entry::find() ->section('locations') ->limit(100000) ->relatedTo([ 'targetElement' => $categoryElement,...


1

Unless I'm missing something - this is how you run through the categories you selected in your Category field when you're looking at that entry's template: {% for cat in entry.yourCategoryFieldhandle.all() %} {{ cat.title }} {% endfor %} EDIT: Ignore this, I can see what you mean. If you select a child category it automatically selects all the parents ...


1

After a ton of headbanging (and not the fun heavy-metal kind) I ended up following the answer in this post, and it seems to be working! Craft 3 Multi Category and Field Search


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