Questions tagged [elementquery]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
3
votes
1answer
275 views

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

I would like to do something like this: craft.entries.section('mySection').field1('> field2').all() And it should return all the entries in mySection whose field1 value is greater than their ...
2
votes
2answers
326 views

Element Query matrixfields where

I'm trying to query entries in a section where the (activities) entry has an item in a matrix field timeSlots -> timeSlot -> startDate. The startDate must be >= NOW(). $query = Entry::find()->...
2
votes
2answers
92 views

Combine AND and OR in a ElementQuery

I have a category with 2 levels. And I want to be able to let the user do a search based on those. The problem is that it needs to be AND between all level 1 categories, and OR within child categories....
2
votes
1answer
75 views

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

EDIT: the below code now works! I have two select inputs, the choice of 1 should populate the second. I have an external twig file (data.twig) (MAKE SURE TO PUT THIS IN YOUR TEMPLATES FOLDER) that ...
2
votes
1answer
167 views

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

I'm confused about the new .one() argument in craft 3 If I want to grab all entries I write: {% for entry in craft.entries.section('thing').all() %} {{ entry.title }} {% endfor %} Alls is nice ...
2
votes
2answers
431 views

Entry Query relatedTo Two Categories

This has to be something that is easy, but I've been beating my head on my desk for a a few hours with this. Most solutions I have come across are way to complex - when all I need to do is query a ...
2
votes
1answer
653 views

How to query entries by field values

I have some entries with a multi-select field called "gallery". We can choose many images to this field. I have a given image, how can I query entries whose "gallery" contains this given image? Thx! ...
2
votes
1answer
53 views

relatedTo() only on one field?

I have a Product entry type that contains a field for Components (which are also products) and a field for Related Products (again, also Products). I want to be able to find a parent product from a ...
2
votes
1answer
70 views

Set OrderBy on Grouped Query Involving Smart Map Field

I'm working with a Structure of resort locations. Each resort has a field address which is a SmartMap field. I'm trying to create an initial display page that groups the resorts based on their state (...
2
votes
1answer
128 views

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

The currentUser.unverifiedEmail variable, which is public, is always empty on front end even after email change. I thought it was a bug. I contacted Craft support about this, and here is Brad's ...
2
votes
1answer
540 views

Craft 3 - Searching by date field - Calling unknown method: craft\behaviors\ElementQueryBehavior

On a website upgraded to Craft 3 - having issues pulling events by date. Error Is: Calling unknown method: craft\behaviors\ElementQueryBehavior::eventDate() The Error occurs on the last line of my ...
2
votes
1answer
139 views

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

I have a nav set up which works find but I need to limit the number of top level links to the first 5 in the loop (right now we have 9 plus oodles of children links!). Have tried various flavours of ...
2
votes
1answer
80 views

What is the correct syntax for using merge to create an array of field values from an element query?

I grab some values via an element query: {% set events = craft.entries.section('events').status('live,pending').order('postDate asc') %} But I need to put them into an array for use in a Craft-...
2
votes
0answers
271 views

Group by Date Field in Element API Query

I have a custom field called releaseDate in one of my entry types. I would like to use Element API to get all the distinct years assigned to those entries. I dump all the years for all entries just ...
1
vote
4answers
490 views

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

Suppose we have a category group that allows for any number of levels to its structure. We also have a category field that allows for choosing any number of categories from that group. In our example,...
1
vote
1answer
1k views

Impossible to invoke a method (“limit”) on an array… but it's not an array?

I am receiving the error message "Impossible to invoke a method ("limit") on an array," but I don't think I am working with an array! Here is why I think that... First I set an albums variable: {% ...
1
vote
1answer
15 views

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

For instance, if I wanted an array of all the titles belonging to a subset of entries that I wish to query. The way I know how to do this currently is like so: {% set titleList = [] %} {% ...
1
vote
1answer
76 views

How to support chained Element queries with Twig tags

The craft variable allows you to chain queries for your Elements. For example, you can query the first Entry in a section with either of the following syntaxes: {% set entry = craft.entries({ section:...
1
vote
1answer
171 views

Difference between chained and batch element queries?

I apologize if this has been asked or addressed already, but I have a question concerning Element Queries. The docs show two possible ways: Chained Methods {% set query = craft.entries() ....
1
vote
1answer
189 views

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

I'm trying to dynamically create an element query in PHP where the custom field criteria come from an array of strings. Something like this...? $users = User::find(); $users->group($groupList); ...
1
vote
1answer
29 views

Check what methods were used on element query

I am creating plugin that provides helper methods for constructing search forms, filtering entry lists by various params etc. And for that plugin, I need to be able to tell which methods were alredy ...
1
vote
2answers
26 views

Matrix fields don't load!

I have some matrix fields defined: But when I use the matrix blocks inside of an entry - I don't get all blocks - just the richText ones. I tested it out - just the richtText Matrix blocks will be ...
1
vote
1answer
263 views

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

Using PHP, I want to update a field of Entries type of an existing entry. Fields of type Text and Number will update with the following code: $entry = Entry::find()->id($entryId)->one(); $...
1
vote
1answer
99 views

What is the appropriate way to query and Hard Delete existing Soft Deleted Elements for a Custom Element?

I have a Custom Element where the Soft Delete functionality is not necessary. Right now, existing users of this plugin may have thousands of Soft Deleted Elements in their Elements table that don't ...
1
vote
1answer
70 views

Date Limit on Element Query

I have a custom date field - "renewalDate" - which I want to use to limit an entry query. Here is the code: When I run that it throws this error: "Calling unknown method: craft\behaviors\...
1
vote
1answer
144 views

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

I've been trying this for a while now, so might have read this before. Sorry about that, I'm struggling really hard to get this right. At first I tried (wrongly) with the eager load features. I have ...
1
vote
1answer
712 views

Craft query Matrix blocks in twig

Is it possible to query elements of type MatrixBlock in Twig like other elements? You can get entries with {% set entries = craft.entries.all() %} Is there something similar available for Matrix ...
1
vote
1answer
361 views

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

How can I filter a for loop based on the value of a field? I tried filterByValue, but I do not know how to construct the statement. This is NOT a category field. I have this... {% for line in ...
1
vote
1answer
241 views

Craft 3 entries “before” not working

I have a Craft 3 site. For some reason the before filter isn't working when pulling entries from a channel, but after does. This is not a data issue. {% set start = "2 months ago"|date() %} {% set ...
1
vote
1answer
24 views

How to exclude multiple email addresses from an order query

When doing an order query I want to omit results for 2 particular email addresses. It's easy enough to omit results for one address; $order = Order::find() ->isCompleted(true) ->email('...
1
vote
1answer
37 views

Simple user query with orderBy not working

Craft 3.3.13 This query does not order users correctly. {% set users = craft.users.groupId(3).orderBy('lastName asc') %} Bug?
1
vote
1answer
68 views

Creating and saving a GlobalSet element type in Craft 3

The following code is in a migration. It runs fine and there are no errors and prints SAVED, but nothing is saved in the globalsets database table. All my other content migrations run fine so it is ...
1
vote
1answer
48 views

Element API: How to get entries up to a certain page?

I have a query that is pulling entries from a structure with elementsPerPage' => 3 If my pagination looks like this: Page 1: Entries: A, B, C Page 2: Entries: D, E, F Page 3: Entries: G, H, I ...
1
vote
1answer
25 views

Array from matrix fields without quotations

I am trying to create an array bodyBlocks by looping through all of my matrix fields called blockTextContent: $bodyBlocks = []; foreach ($entry->getFieldValue('pageBuilder')->all() as $block){ ...
1
vote
1answer
69 views

relatedTo - return only entries that have a related entry where a field in the related entry has a value

Trying to figure out if this is possible in 1 step. I want to return locations that have a related entry in the relatedMerchant field, but I'd also want to make sure that a field in the ...
1
vote
1answer
23 views

Checking if user authored an entry in another channel while in loop, N+1 problem

I have 2 channels set up: Channel A and Channel B. I also have public registration so users can log in and fill out two forms (Form A and Form B), each form creating an entry in their respective ...
1
vote
1answer
307 views

Craft 3 - Programatically get all the values contained in a field of type category

I want to programmatically get all the values contained in a field of type category. I am doing this on Elements::EVENT_AFTER_SAVE_ELEMENT. If I dump $event->element->getFieldValue('...
1
vote
1answer
61 views

How to get entries with or

it is possible to do like this: $users = Entry::find() ->userLevel('>3') ->or group('admin') So in database level,the sql will go like this: select * from users where users.userLevel ...
1
vote
0answers
21 views

Freeform: query form object for presence of field type (or other criterion)

I want to be able to check if a form object returned by a query has fields of a certain scope before I generate its markup. I have a test case where I want to know if a form has a checkbox field type ...
1
vote
1answer
126 views

How do I flush the query cache?

How do I flush Craft's query cache so that I may query a relationship field again in the same web request with a new set of parameters? In this particular scenario, I need to handle it inside of a ...
1
vote
0answers
73 views

Advanced queries with assets and other custom fields

To optimize performance, I'm trying to create an advanced entry query that selects only specific variables. I can set up the query to grab the entry title, uri, and a custom variable I created ("email"...
1
vote
0answers
68 views

What's the most performant way to get reverse related entries within an Element Query?

What's the most performant way to get reverse related entries within an Element Query? Here's a contrived example of this. I have thousands of "book" entries and thousands of "collection" entries. I ...
1
vote
0answers
53 views

Order entries by a matrix field category

I have a lot of trouble in understanding the database/model structure and how to work with the query builder. I tried playing a bit with leftJoins but i cannot seem to make complex queries in craft 3. ...
1
vote
0answers
155 views

How do you query entries by field value for multi-select field type? [duplicate]

I am looking to query entries by a field value. The value will be a dynamic variable but i cannot get any results showing with hardcoded state. This is the code i have so far: $entries = Entry::...
0
votes
1answer
30 views

getVersionsByEntryId is deprecated. Can't find the new method?

We are using getVersionsByEntryId in our XML sitemap to return the number of times an entry has been saved, then comparing that with todays date and the date the entry was created to determine the ...
0
votes
1answer
495 views

Twig: length giving 1 on empty array?

This code worked fine in Craft 2: {% set entries = craft.entries.section('events').relatedTo(category).orderBy('startDate asc').limit(3) %} {% set extraEntries = craft.entries.section('events')....
0
votes
0answers
18 views

Order by dynamic field in custom Element Type

In my module, I have a custom Element Type with three fields (firstName, lastName, email) that are dispalyed on an element index page in the CP. I have the first column as a full name by concating the ...
0
votes
0answers
33 views

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

So I have structures that are related in the follwing way: Locations has many Providers Providers has many Health Topics I know that I can eager load all Locations with related Providers and Health ...
0
votes
0answers
29 views

Apply search filter in Element API

I have managed to build the search filter structure at the twig, but I get stuck when try to convert this structure in element API, {% if craft.request.getParam('query') %} {% set keyword = craft....
0
votes
0answers
20 views

Entry query in Twig setGlobals() not populating custom field data

I am using a custom Twig extension to add global variables: class MyGlobals extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { public function getGlobals() { // This ...