Skip to main content

Questions tagged [elementquery]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4 votes
1 answer
309 views

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

With element queries, we can search for elements using search method: craft.entries.search('someField:something') or by using field handle as method: craft.entries.someField('something') How these ...
Piotr Pogorzelski's user avatar
3 votes
2 answers
1k 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(); $...
biko's user avatar
  • 670
3 votes
2 answers
1k 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 ...
benface's user avatar
  • 226
3 votes
1 answer
1k 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! ...
yu xia's user avatar
  • 397
3 votes
1 answer
160 views

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

I have a scenario where level 1 structure entries each have several to tens of level 2 child entries, and several to tens of entries in a different channel which are also allowed to be related to the ...
Jonathan Schofield's user avatar
3 votes
1 answer
192 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 ...
Tony DeStefano's user avatar
2 votes
2 answers
1k views

Element query for specific fields

Can a return schema be specified for entry queries? I'd only like the titles instead of the whole entry.
ponies's user avatar
  • 387
2 votes
1 answer
3k 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: {% ...
Steven Thate's user avatar
2 votes
2 answers
622 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()->...
Jeroen's user avatar
  • 308
2 votes
1 answer
970 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 ...
KSPR's user avatar
  • 3,786
2 votes
1 answer
2k views

Advanced Element Queries - example uses

In Craft docs element queries article, there is section "advanced element queries", with methods like where, groupBy etc. What are example use cases of these methods in Twig templates or PHP ...
Piotr Pogorzelski's user avatar
2 votes
2 answers
132 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....
peirix's user avatar
  • 431
2 votes
2 answers
2k 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 ...
gjhead's user avatar
  • 123
2 votes
2 answers
151 views

Get children of multiple entries

I'm sure this must be simple but I've tried multiple options with little success. What is the best way to build a query to get the children of more than one entry via an entry field? For one parent ...
SplendidRob's user avatar
2 votes
2 answers
264 views

Getting an error on element query after upgrade to craft 4

I've upgraded a project to craft 4, and now I'm getting an error on my entry query. Below is the code I have: {% set queryFeaturedEvents = entry.featuredEventsLimited.limit(3) %} {% set ...
Julie Velghe's user avatar
2 votes
1 answer
301 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 ...
Myles Hyson's user avatar
2 votes
1 answer
1k 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 ...
Niels's user avatar
  • 75
2 votes
1 answer
161 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 Smart Map field. I'm trying to create an initial display page that groups the resorts based on their state (...
Tim Knight's user avatar
  • 1,022
2 votes
1 answer
180 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 ...
Romain P.'s user avatar
  • 1,852
2 votes
1 answer
828 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 ...
mmc501's user avatar
  • 1,789
2 votes
1 answer
244 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 ...
Brandon Kelly's user avatar
2 votes
1 answer
150 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-...
4midori's user avatar
  • 656
2 votes
0 answers
173 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 ...
Aaron Bushnell's user avatar
2 votes
0 answers
450 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 ...
Tony DeStefano's user avatar
1 vote
2 answers
321 views

Cannot pass where Params to query with Twig variable

I have a text field that has three values on new lines. E01001667 E01001668 E01000779 I want to use these for a query that looks like this: {% set mylsoas = craft.entries. section('imdData') ....
joomkit's user avatar
  • 2,034
1 vote
4 answers
2k 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,...
Benjamin Kohl's user avatar
1 vote
1 answer
74 views

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

This seems more like a PHP question than a Craft CMS question, but since it's related to an entry query I'm trying to create I figured someone from the Craft community might know. I'm having trouble ...
Dalton Rooney's user avatar
1 vote
1 answer
232 views

Collections groupBy

This feels like a really dumb one...when I'm grouping a collection, how do I loop over it? {% set entries = craft.entries().collect() %} {% set groupBySection = entries.groupBy('section.handle') %} {% ...
supazu's user avatar
  • 526
1 vote
2 answers
99 views

OrderBy section when selecting multiple sections in entries query

When getting pages from multiple sections like so: {% set aboutNavigation = craft.entries .section(['about', 'newsIndex']) .all() %} How can I set order that the sections appear? So I want ...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
185 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 = [] %} {% ...
Brimby's user avatar
  • 343
1 vote
2 answers
167 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?
4midori's user avatar
  • 656
1 vote
1 answer
243 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:...
Ben Parizek's user avatar
  • 13.5k
1 vote
1 answer
1k 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 ...
Vin's user avatar
  • 383
1 vote
1 answer
225 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() ....
Steven Thate's user avatar
1 vote
1 answer
453 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); ...
Mark J. Reeves's user avatar
1 vote
1 answer
109 views

Do cache queries work on relation fields and global sets?

I see in the docs I can do something like: {% set entries = craft.entries .sectionId(2) .with(['someRelationField']) .cache(0) .all() %} and this will cache the query indefinitely because of ...
Ryan's user avatar
  • 1,952
1 vote
1 answer
40 views

Why is my relational query returning zero?

I have an entries page structure section Pages And two structure sections / menus Menu A Menu B Menu A and Menu B have an entry field link called 'entryLink' that is a relation to an entry in the page ...
joomkit's user avatar
  • 2,034
1 vote
1 answer
117 views

Is there a way to query for structure Entries that have no Parent?

Suppose I have a structure Section with Entries like this: Parent A Child X Parent B Parent C Child Y How would I return this list of Entries? [Parent A, Parent B, Parent C]
sam's user avatar
  • 11
1 vote
1 answer
129 views

Get raw/unformatted field values?

Is there a way to get raw/unformatted values for a field before they're passed to the field's normalizeValue function? That is, get the value close to how it's stored in the database? I haven't found ...
MoritzLost's user avatar
  • 11.5k
1 vote
1 answer
78 views

Ordering products using custom field and pagination

I have element query that orders products by custom field which is numeric. Default value of this field is "1". {% set products = products.relatedTo(category).orderBy('productOrderInCategory'...
Piotr Pogorzelski's user avatar
1 vote
1 answer
601 views

Custom Module: new element type index page access

Hello I've created a new element type in a module (a web app within Craft) that I'm developing. I have initially created my models to be craft\db\ActiveRecord and save directly to their tables, but ...
TechMafioso's user avatar
1 vote
1 answer
53 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 ...
Piotr Pogorzelski's user avatar
1 vote
2 answers
55 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 ...
Slowwie's user avatar
  • 235
1 vote
1 answer
455 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 ...
Ben Parizek's user avatar
  • 13.5k
1 vote
1 answer
111 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\...
Roi Agneta's user avatar
  • 1,463
1 vote
1 answer
287 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 ...
Jorge Anzola's user avatar
1 vote
1 answer
637 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 ...
Hellyar's user avatar
  • 921
1 vote
1 answer
273 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 ...
JamesNZ's user avatar
  • 947
1 vote
1 answer
146 views

How best to merge an array into an array for a relatedTo query parameter?

I have 3 Matrix fields which define optional parameters for building a query: A related Entries field A Categories field A Tags field Any combination of them could be used, including none or all. So ...
Jonathan Schofield's user avatar
1 vote
1 answer
107 views

How do you save custom fields on an EntryFixture?

I can't for the life of me figure out how to setup EntryFixtures in craft to populate with custom fields. I'm using the project config which already has the sections and fields defined in my tests. I'...
Myles Hyson's user avatar