Questions tagged [elementquery]
The elementquery tag has no usage guidance.
64
questions
0
votes
1
answer
25
views
Sort entries by separate related entries count
I am working on a sprig board that filters out Lessons from a structure in Craft.
This screenshot better illustrates this and how it works.
The main table is built using the following:
{# Sets default ...
2
votes
2
answers
139
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.
1
vote
2
answers
50
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')
....
1
vote
1
answer
33
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 ...
1
vote
2
answers
36
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 ...
1
vote
1
answer
61
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]
0
votes
1
answer
60
views
Find related entries by specific position in the entries field
I'm looking for a way to limit a reverse relations query to only match the first selected entry in an entries field.
Some context: I have two entry types, institutes and offers. Offers have an ...
1
vote
1
answer
47
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 ...
1
vote
1
answer
45
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'...
3
votes
1
answer
101
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 ...
1
vote
1
answer
86
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'...
1
vote
1
answer
60
views
Commerce element query on custom field in console command
I'm on Craft 3.4.30 (latest 3.4) and Commerce 2.2.25 (latest 2.2):
My product type "ticket" has a custom date field with handle productStartDate.
E.g. the following query in TWIG works:
{% ...
2
votes
1
answer
588
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 ...
4
votes
1
answer
123
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 ...
1
vote
1
answer
335
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 ...
1
vote
1
answer
23
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
1
answer
50
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
2
answers
37
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
0
answers
42
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
1
answer
132
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 ...
2
votes
1
answer
668
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 ...
1
vote
1
answer
31
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('...
2
votes
2
answers
106
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....
3
votes
2
answers
978
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
1
answer
98
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?
2
votes
1
answer
546
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 ...
0
votes
1
answer
57
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 ...
1
vote
1
answer
149
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
1
answer
104
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
1
answer
37
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
1
answer
429
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
1
answer
284
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
0
answers
214
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
4
answers
1k
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,...
0
votes
1
answer
260
views
How do I fetch all categories under a category group using the Element API?
I am not too familiar with PHP and am just learning the Element API.
How do I turn this craft query into an element api query?
craft.categories.group("brands").level(1).all(),
In categories this is ...
2
votes
2
answers
949
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 ...
1
vote
1
answer
89
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
1
answer
186
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:...
2
votes
0
answers
127
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 ...
3
votes
1
answer
82
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 ...
0
votes
0
answers
72
views
How to add an Element sortOption to sort by number?
I have an Element. It's a Structure, with one level (not sure if that is relevant). I'd like to add a Sort Option that allows a user to sort the Element Index page for this Element by a number. The ...
1
vote
0
answers
74
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. ...
2
votes
0
answers
390
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
1
answer
33
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 ...
3
votes
2
answers
748
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 ...
1
vote
1
answer
91
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\...
2
votes
1
answer
128
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 (...
2
votes
1
answer
2k
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:
{% ...
2
votes
1
answer
932
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!
...
1
vote
1
answer
240
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 ...