Questions tagged [elementcriteriamodel]

Questions having to do with Craft's ElementCriteriaModel, which is used to return a set of elements narrowed by a variety of parameters.

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

Modify ElementCriteria and use it for paginate

First some context. I'm using Smart Map for Craft to display some location based events in a listing. My structure is as follows: Events channel Locations channel Each event can occur multiple times,...
3 votes
2 answers
321 views

Smart Map - Sorting for closest location in PHP

I want to sort entries with a Smart Map address field for closest location. I know how it should be done in a template: {% set entries = craft.entries.myAddress(params).order('distance').find() %} ...
3 votes
1 answer
105 views

Filtering/Searching fields with enumerated data (e.g. Smart Map) in an ElementCriteria model

I have an ElementCriteriaModel in which I need to find elements that contain a particular value for "state", which is a property of a "Smart Map" field ($element->smartMapField->state). ...
1 vote
1 answer
397 views

Check for value in supertable

Is it possible to check for a value in a supertable field during the for loop: {% for item in craft.products.supertable.lightswitch(1).all() %} {# stuff #} {% endfor %} This returns Impossible ...
1 vote
1 answer
95 views

Elements API - How to order / limit the output with an asset field of an entry

I recently started using the Elements API and I made up a recipe site as an example project. It's pretty easy to output the recipes as json data. But I now want some more control about the output of ...
6 votes
2 answers
707 views

Ability to SUM() or GROUP BY from the template or ElementCriteriaModel

After twitter conversation with Brandon Kelly, is it possible to implement something similar to the following? craft.entries.section("mySection").sum("myCustomField")
2 votes
1 answer
741 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
0 answers
47 views

Setting a variable to a Matrix field with additional parameters

I'm trying to set a variable to a Matrix field and apply additional query parameters like so: {% set monthDates = entry.scanDates.type('dates').date('and', '>= ' ~ currMonthFirst|date('Y-m-d'), '&...
1 vote
0 answers
57 views

Retrieving Entries by Category & Exclude by Tag

I have an endpoint that I want to retrieve entries by a category, but then exclude entry ids if they have a certain tag. It seems to me that the relatedTo param overrides the id param that says not in ...
2 votes
1 answer
686 views

How to merge multiple 'relatedTo' queries via query parameters

I'm trying to augment a Commerce query with additional parameters like so, but I can't get 'colour' and 'range' working together. Clearly my and/relatedTo syntax is wrong but how exactly? {% set ...
1 vote
1 answer
249 views

Eager Loading Image Transforms In Plugin

The craft docs on eager loading show you how to eager load image transforms in twig. {% set entries = craft.entries({ with: [ ['coolImage', { withTransforms: ['transform'] }] ] }) %} ...
1 vote
1 answer
369 views

Multiple and or relatedTo in plugin element criteria

I am trying to return entries that relate to a series of categories. For example, I am trying to return clothing, in this example, I want to return all entries related to (flannel or denim) and (blue ...
1 vote
1 answer
43 views

Getting orders by property in Commerce 2 from Plugin

In Commerce 1 i can select orders by a specific property as follows: $criteria = craft()->elements->getCriteria('Commerce_Order'); $criteria['orderStatusId'] = 1; return craft()->elements-&...
1 vote
1 answer
491 views

Get all variants from multiple products

I need to get a list of variants from multiple products, but in a way that allows us to show the results in Craft's handy {% paginate %} tags. So far we have this less-than-appealing markup: {% set ...
2 votes
1 answer
40 views

ElementCriteriaModel combine with 'and' and 'or' filters

Saying I want to convert the following SQL query into ElementCriteriaModel: SELECT * FROM users WHERE fieldA = 1 AND (fieldB = 1 or fieldC = 1) I can't find a way in ElementCriteriaModel to do so. {...
1 vote
0 answers
30 views

modifyEntrySources hook with reverse relations

Is it possible to do a reverse relation query in a custom entry source? https://docs.craftcms.com/v2/plugins/hooks-reference.html#modifyentrysources I have two entry sections, one called courses and ...
1 vote
1 answer
40 views

modifyEntrySources hook, comparing two field values

Is it possible with the modifyEntrySources hook to query a field value against another fields value? Example: Two fields are present on the entry section this hook targets fieldParentId fieldId I ...
2 votes
1 answer
49 views

Plugin, getting an entry when one of multiple category fields and/or lightswitches are not empty/turned on

Basically I’m trying to get en Entry where, either one (or more) of three lightswitches is (are) turned on, or where either categoryOne or categoryTwo is not empty. This is what I got so far, but it ...
1 vote
1 answer
80 views

Pagination and Entry Model on Homepage

On my site I have some entries with images. In the entry template I paginate the images like so: {% paginate entry.images.limit(1) as images %} My homepage is a single random entry and I want to ...
1 vote
1 answer
184 views

Element criteria with a table field

I am trying to build a query to fetch some entries which I want filter on a table field between multiple values. The table field is "filterTestWidth" and has 1 col named "size" (number type). There ...
7 votes
1 answer
1k views

Eager loading of reverse related elements?

Is it possible to load reverse related elements? I mean if the elements I’m querying are the target elements? Example: I’m setting up a criteria model to get some categories and I also want to load ...
1 vote
0 answers
31 views

Get Entries with a specified. Custom FieldType

I have created a Custom FieldType with AttributeType::Mixed The field actually saves the a Custom Model I created. I'm trying to retrieve all the entries that have my custom model as the field. I'm ...
0 votes
1 answer
772 views

Impossible to access an attribute ("url") on a null variable in search results

I have a search results page set up like this: {% set searchResults = craft.entries(queryParams) %} {% paginate searchResults as pageInfo, pageEntries %} {% if pageEntries|length %} {% for yacht ...
1 vote
1 answer
142 views

no element type exists with getCriteria and custom element type

I am working on a plugin using the getCriteria function, but for every Element Type I put, I always get an error: No element type exists by the type “Comerce_Group”. $criteria = craft()->...
3 votes
1 answer
810 views

How can I query for entries that have one of two Lightswitch fields enabled?

I have two Lightswitch fields – lightswitchA and lightswitchB – and I need to pull all entries in a certain section, that have one or both of these fields enabled. Is it possible to execute such a ...
1 vote
1 answer
424 views

Get the fields (fieldLayout) of the section. Not the entry

Can I get all the fields of a section? For example, I have channel events, I want the fields in that section, but not related to any specific event. I want to know all the fields (not the values), ...
1 vote
0 answers
495 views

Element API plugin: searching and working with nested objects

I’m experimenting with the Element API plugin and working with querying the tags of the assets that appear in matrix blocks in a set of entries. Essentially I’m hoping to be able to use a tag in a ...
1 vote
1 answer
382 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); ...
4 votes
1 answer
288 views

Get entries that's not already listed

I have a bunch of entries in a section called 'artikkel'. The 14 first of these entries are listed out in another section called 'forside'. Now I need to list out the entries that's not already been ...
0 votes
1 answer
1k views

Get all entries with linked entry in entries field

I have a channel called 'projects'. This channel has two entries fields: relatedThemes relatedAreas What I want to do is perform a search query which filters all projects where the relatedThemes ...
2 votes
1 answer
223 views

Missing Matrix row types on output after upgrade to Craft 3 (subsetting and cloning ElementQueries)

Update: Now I know the cause of my problem, I have appended to the original question title to try and help other people stumbling across the same issue. I have a pending support ticket open with P&...
1 vote
1 answer
27 views

Not showing the entry somewhere else on the same page

In the footer there is a column showing the images of the most recent entries. How do you prevent the image from the main entry itself to be shown? Do you test an url segment against the title or ...
1 vote
1 answer
28 views

Error using assets operations

I am having trouble getting images by name. craft.assets.first() function in assets gives the proper output of one image. But when I try craft.assets.filename('flag.jpg'), craft shows the following ...
1 vote
1 answer
117 views

How to get related entry data using ElementCriteriaModel in a plugin

I am working on a custom plugin which gets some entry data using $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->order('postDate asc'); $criteria->dateUpdated = ...
1 vote
1 answer
57 views

How to limit navigation by entry type

I couldn't find this anywhere so I thought I would post the answer. How do you limit the output of a navigation element to a specific entry type. This question is explicitly for the navigation ...
1 vote
1 answer
826 views

How to build complex Element Criteria?

How to fetch Elements with a complex DB query? E.g. with some nested AND/OR conditions or just apply two conditions to one field? For example my Elements have a field eventDate. I want to fetch all ...
1 vote
1 answer
506 views

Returning ElementCriteriaModel of entries via plugin

I'm outputting an element criteria model in a Craft 2 plugin of some entries in a channel. I noticed that when I fetch the entries the standard template way {% set entries = craft.entries.section('...
3 votes
1 answer
345 views

Search for entries where a field value is between multiple ranges of numbers

I currently have this working for a single range of numbers, but I can’t figure it out for multiple ranges. I currently have it set up like this for one range, and it works. myFieldHandle: ['and', '&...
4 votes
1 answer
107 views

Store categories in array by level in controller

I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group. So I used the following code to ...
1 vote
1 answer
345 views

How can I iterate through my entries?

I want to iterate through my entries on the click of a 'next' and 'previous' button. I have started off using getNext() and getPrev() by I'm in a bit of a rut. {% set entry = craft.entries.section('...
5 votes
3 answers
775 views

Template Hook + Caching

A portion of my template is being cached, using the standard cache tag: {% cache %} Within that cached section of the template, I'm using a template hook: {% hook 'myTemplateHook' %} Within that ...
1 vote
2 answers
102 views

Getting a single, random, related entry within a plug in

Given a Matrix with each row containing a text field and a multi relationship field. And I am picking 1 Matrix row based on the value in the text field. How would I go about selecting a single, random,...
1 vote
0 answers
147 views

Using target element criteria to filter results

I'm using the Element API plugin to try and search for addresses using minimum and maximum lat & lng values and then using target element within the criteria parameters to filter the results ...
1 vote
2 answers
248 views

Retrieve entries on custom field date using ElementCriteriaModel

I am trying to retrieve an array of entries on a particular section where inspectionDate is greater than today's date. Here is the code. $criteria = craft()->elements->getCriteria(ElementType::...
4 votes
2 answers
2k views

How do I find the closest parent entry with an Assets field filled in?

I have a structure that goes up to 4 levels deep. Each page has a field for a 'hero' image. If a child page does not have this field filled out I would like to traverse up the structure and find the ...
1 vote
0 answers
172 views

If an entry doesn't exist in a locale and returns a 404, how can I check if the entry exists in a different locale?

I'm trying to create a mechanism for informing users that a particular entry is not available in the selected language, and providing a link to read the article in an alternative language. Therefore, ...
1 vote
1 answer
70 views

Load more images in matrix block

I have a matrix block called team that has an assets field within it that has all of the companies headshots. However, there are more than 30 images, but the design is to only have 6 showing on load, ...
1 vote
1 answer
370 views

ElementCriteriaModel andWhere() with different comparison operators?

I'm trying to do an andWhere() "OR" group for a special criteria value so basically when building an element criteria I want to just set `$criteria->upcomingOnly to true, and then have the following ...
22 votes
3 answers
4k views

How can I get only those entries with non-empty assets field

Since Craft 2.0 it is possible to pass :empty: and :notempty: to ElementCriteriaModel parameters when you’re looking for empty/non-empty values. If you are for example looking for all entries with a ...
1 vote
1 answer
278 views

getting the current user entries from elementapi

How do I use the elementapi to get the list of entries for the logged in user? I've tried a couple of options to no avail, like 'api/v1/user/<userId:\d+>.json' => function($userId) { ...