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
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 ...
  • 36k
15 votes
1 answer
1k views

How do you sort elements queried from multiple channels?

This is how I query elements from multiple channels: {% set allEntries = craft.entries.section(['publication','text', 'exhibition']).limit(null) %} I now want to sort all these entries by a custom ...
  • 36k
12 votes
2 answers
1k views

What are the differences between an elementCriteriaModel and fetched elements?

I am a little confused about the differences between an elementCriteriaModel and fetched elements. Why do I sometimes have to fetch elements with first(), last(), find(): {% set entries = craft....
  • 8,346
12 votes
2 answers
1k views

How to find entries with no category

I have a field categories in a channel news, and I want to find all the entries in news that don't have a category set. I know I could loop over the entries and find them that way, but I'm hoping to ...
11 votes
3 answers
3k views

How to search for a specific field using getCriteria()?

I'm looking at making a plugin and need to search news entries. I can use: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'news'; $find = $criteria->...
  • 375
8 votes
1 answer
9k views

Order entries by date field

I have a channel section with a handle named 'events'. In my field layout for this section I have a field named eventsDate, which is a Date/Time field. I also have a plain text field called '...
  • 605
7 votes
1 answer
1k views

How can I do more complicated queries with an ElementCriteriaModel?

I need to join my 3rd party table with a standard ElementCriteriaModel (ECM). I'd like to start with an ordinary ECM, so my plugin will be compatible with any element type which may exist, now or in ...
  • 23.8k
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 ...
  • 36k
6 votes
3 answers
856 views

Fetch entries with special characters in title

When trying to fetch an entry with special characters in the title, it doesn't seem to match. $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->title = "Title,with,...
  • 3,507
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")
6 votes
1 answer
517 views

Display thumbnail using defineTableAttributes for a custom Element Type

Hi, I'm new to Craft and VERY new to plugin development. I'd like to display a thumbnail image in my table list view and thought it would be possible to accomplish using a new Custom Element Type. ...
  • 121
6 votes
1 answer
1k views

How can I follow relations in ElementCriteriaModels from plugin code?

In Twig code you can use craft->entries->relatedTo to do all sorts of neat things. From plugin code I am not sure how this works? $c = craft()->elements->getCriteria(ElementType::Entry)...
  • 631
6 votes
1 answer
901 views

Get related entry data within a plugin (ElementCriteriaModel) without extra queries

I have the following code: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'submissions'; // Loop foreach ($criteria as $entry) { echo $entry->id....
6 votes
1 answer
675 views

How can I return specific matrix rows based on criteria?

So let's say I have the following code. I have the data to retrieve the entry I want, But I only want to return some of the matrix rows based on some criteria. Example Code: {% set entryId = 1 %} {% ...
5 votes
1 answer
594 views

Get the type of action and the elements(s) that an action is being performed on when listening to elements.onPerformAction?

I have a plugin that needs to do something when an entry is deleted. I've recently discovered that the entries.deleteEntry event is not fired when deleting bulk entries (the list view with checkboxes) ...
  • 373
5 votes
1 answer
1k views

How do I get elements using getCriteria from a specific date?

I'm trying to get elements in a section created on a specific date. How do I do that? This is what I've been trying with: $criteria = craft()->elements->getCriteria(ElementType::...
  • 2,834
5 votes
2 answers
528 views

Fetch all unused assets?

How could I go about fetching all assets that are not currently beeing used anywhere?
  • 36k
5 votes
1 answer
732 views

How can you return elements between two values?

I my be in a bit of a "wood for the trees" situation but how is it possible to return elements based on a custom field being between two supplied values. For example returning users based on a number ...
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 ...
  • 23.8k
5 votes
2 answers
1k views

How to retrieve entries from two entry-types ordered by different date-fields?

I have two entry-types, 'Events' and 'Reports'. 'Events' have a matrix-field ('eventMultiDate') that allows authors to assign multiple dates to an event (recurring). 'Reports' have the default '...
5 votes
1 answer
172 views

Many entry fields, only one db request?

Craft 101: I have entries with many different fields, so I'm doing something like this: {% set var = craft.entries.section('foo').slug('bar').first() %} {% if var %} {{ var.field1 }} {{ var....
  • 339
5 votes
1 answer
1k views

How can I create an ElementCriteriaModel with 'and' and 'or'

In my template, I want to: Get all the entries in the section 'uploads'; Get all the assets that are related to one of the upload entries, and have a relation to all the tags I provide. So, that ...
  • 6,318
5 votes
1 answer
637 views

In the Element API plugin, how can I get only tags that are related to a particular section?

I tried to get all tags that are related to an section. Everything I tried failed. Is there a way to get all assigned tags for a particular element type by using element criteria model. In this case ...
5 votes
1 answer
889 views

How to fetch matrix block items with the ElementAPI

I have a question regarding the ElementAPI: I am sucessfully using the ElementAPI to fetch assets related to an entry. Now I have a matrix field where a user can add multiple galleries (multiple ...
  • 383
5 votes
1 answer
1k views

Returning specific fields with elementCriteriaModel

I'm writing a plugin that adds or updates users profiles in Craft from a JSON file. Every user profile could have a lot of custom fields in the future which store a lot of data. To check if a user ...
  • 1,456
5 votes
1 answer
251 views

How could I make related entries load faster?

I need to show a list of users (around 400). No problem, the list loads in 1.5s, for which I could use {% cache %} to speed it up. Now, the problem arises whenever I want to display the title of an ...
  • 6,318
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 ...
  • 43
4 votes
1 answer
1k views

Complex logic on a ElementCriteriaModel parameter?

How would you combine multiple logical expressions on a ElementCriteriaModel parameter? You could query twice and then merge the two returned arrays of entry models, but that probably wouldn't allow ...
  • 36k
4 votes
1 answer
915 views

How can I output a list of categories that are not empty?

In other words, categories must have at least one enabled entry assigned to them.
  • 1,503
4 votes
1 answer
1k views

Build Query relatedTo multiple categories

Trying to build a complex query and I'm sure there is bound to be a better way of doing it that the multiple if statements I currently have. URL for the page is /products/(category1)/(category2)/(...
  • 1,779
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,...
  • 137
4 votes
1 answer
772 views

Using search to display entries based on a value in a number field

I'm trying to return results based on a year stored in a custom field. For example, returning entries with a model year greater than 2006 and less than 2009. Is there a search syntax to support this? ...
  • 367
4 votes
2 answers
479 views

What's the difference between ElementCriteriaModel and DbCommand?

Forgive me if this is a stupid or naive question... I generally think of an ElementCriteriaModel as being a "search object". Specifically, it's a search object which revolves around a specific type ...
  • 23.8k
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 ...
  • 105
4 votes
1 answer
175 views

Is it possible for an Element Type to return multiple content tables with the getContentTableForElementsQuery() method?

I'm building a plugin where each Element created has a unique content table associated with it. Each individual Element behaves as needed, however, when I try to create an index page that returns all ...
  • 13.3k
4 votes
1 answer
763 views

Query to element criteria

Is it possible to turn a query into an element criteria model? Currently I'm doing this: $criteria = craft()->elements->getCriteria('Conference_Session'); $criteria->conferenceId = $...
  • 491
4 votes
1 answer
962 views

Related to entries in categories in a plugin

Ok I can do this in twig but after searching high and low I can't find a good example of how to do it in a plugin. I have a user profile field called interests which holds an array of what they are ...
  • 2,033
4 votes
1 answer
448 views

How to create a query to group alternate language pages?

I'm trying to create a sitemap for a site with multiple locales. An example of the format is described here, but as a quick summary: I need to output locales of related translated pages aside one ...
  • 13.3k
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 ...
4 votes
1 answer
450 views

multi-select OR values to filter entries

preface: i'm new to craft so may get terminology incorrect basic idea: i would like to show entries of a particular channel which have multi-select field values which intersect with another set of ...
  • 141
3 votes
1 answer
819 views

Infinite getPrev and getNext?

I know how to display every next entry, but is it possible when there is no next entry it displays the first one and if there isn’t a previous display the last entry?
  • 36k
3 votes
1 answer
805 views

ElementCriteriaModel exclude list of ids

I'm trying to exclude a list of ids from a query using an ElementCriteriaModel This is what I have so far, but it's resulting in nothing coming back when I'm expecting at least 1 result. Is something ...
user avatar
3 votes
1 answer
817 views

Ordering of ElementCriteriaModel with varying statuses

I have a built a little plugin that calls the Twitter API to add new Tweets from a user timeline as entries, so that I can return them in an ElementCriteriaModel request to be integrated with content ...
  • 945
3 votes
1 answer
63 views

Performance when loading lots of Elements but only need limited data

The performance of the ElementCriteriaModel for lookups is killing me. I have 300 or so "People" entries in a craft site and trying to load them just for a basic listing page takes forever. There's ...
  • 917
3 votes
1 answer
81 views

A more economical method of building an archive menu?

I seem to be having trouble with the performance of my site (on localhost) after including this object. Im using it to build a menu for an archive of the posts section which has about 2,500 entries ...
  • 45
3 votes
1 answer
485 views

Querying for an exact match on a Matrix block field with multiple types

I'm trying to query against a specific field value in a Matrix field with multiple block types, and I've tried multiple approaches with no success. It either returns 0 results, or all blocks ...
  • 2,957
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
917 views

Add custom element criteria attribute without a field type?

Is a custom field type and making use of modifyElementsQuery the only way to add my own craft.entries element criteria model attribute to modify the elements query? What if I do not want to add a new ...
  • 36k
3 votes
1 answer
134 views

How do I query two relationship fields where one relationship MUST be empty?

I have two relationship fields. I am trying to query all entries where the first field is related to a particular element and the second field is related to nothing. Here is how I thought it would ...
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 ...