19 votes
Accepted

What are the differences between an elementCriteriaModel and fetched elements?

The ElementCriteriaModel and the individual element models (like the EntryModel) can be thought of as two different points of time in retrieving data from your database. Essentially, you'll use an ...
user avatar
18 votes
Accepted

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

Craft 2.5 introduced a new feature for this. It’s now possible to find elements where a relational field does/doesn’t have any related elements, by passing ':notempty:' or ':empty:' onto the field’...
user avatar
  • 35.8k
13 votes
Accepted

Order entries by date field

Read the article about ElementCriteriaModel in the official Craft docs on how to query elements in general and the craft.entries article about the parameter available to modify the object returned. ...
user avatar
  • 35.8k
11 votes
Accepted

How to find entries with no category

You can do this in a single query using the search parameter: {% set uncategorized = craft.entries.section('news').search('-categories:*') %} More on searching here.
user avatar
  • 6,146
10 votes
Accepted

How can I do more complicated queries with an ElementCriteriaModel?

Once you've got your ECM, pass it off to the buildElementsQuery method... $query = craft()->elements->buildElementsQuery($criteria); This will convert your ECM into a DbCommand object. From ...
user avatar
  • 23.6k
10 votes

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

As per the (new) "Working with Elements" guide from the docs, it works the same as from your templates. The actual list of available parameters depends on the element type. They are identical to ...
user avatar
  • 35.8k
9 votes
Accepted

Complex logic on a ElementCriteriaModel parameter?

For most cases you can find a workaround to combine such conditions by making use of entry IDs. In this example query for each time range condition separately and use the ids() method to "collect" ...
user avatar
  • 35.8k
9 votes
Accepted

How do you sort elements queried from multiple channels?

The order param sets the ORDER BY part of the MySQL query. You can also pass MySQL functions in it. In your case GREATEST() should do the job: {% set allEntries = craft.entries .section(['...
user avatar
  • 8,316
9 votes
Accepted

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

Something like this should work: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'sectionHandle'; $criteria->postDate = array('and', '> 2014-11-03 ...
user avatar
  • 66.4k
9 votes
Accepted

Fetch entries with special characters in title

Have a look at DbHelper::parseParam docs: If the $value is a string, it will automatically be converted to an array, split on any commas within the string (via ArrayHelper::stringToArray()). If ...
user avatar
  • 35.8k
9 votes
Accepted

Infinite getPrev and getNext?

You would need query for these entries using the first and the last method. {% set criteria = craft.entries.section('blog').order('title asc') %} {% set prevEntry = entry.getPrev(criteria) ?: ...
user avatar
  • 35.8k
9 votes

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

It appears that when one or more Entries (or other types of content) are manipulated (in my case, deleted) from the bulk edit view in the Control Panel, the type of events that are triggered are ...
user avatar
  • 373
8 votes

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

You can do this with the relatedTo param. First get all assets that could possibly be related. (If you only wanted to find entries that related assets from a particular source, this would be the ...
user avatar
7 votes

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

You can use the a special search format. $criteria-> search = 'fieldHandle:searchstring'; Learn more about searching Hope that helps!
user avatar
  • 1,623
7 votes
Accepted

Or element criteria model filters

I think that you actually want to use 'and' rather than 'or'. 'Or' would return all entries. This is also assuming that you have predefined the 'startDate' and 'endDate' variables. {% set params = { ...
user avatar
7 votes
Accepted

Fetch all unused assets?

With good help from @carlcs here and on Slack, we found a solution: {% set allEntryIds = craft.entries.limit(null).ids() %} {% set usedAssets = craft.assets({ relatedTo: allEntryIds, limit: ...
user avatar
  • 1,332
6 votes

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

You could do this with a plugin. Create a Variable class with a function called sum(), which accepts an ElementCriteriaModel, passes it to ElementsService::buildElementsQuery(), and then replaces the ...
user avatar
6 votes
Accepted

How can you return elements between two values?

You’re close! You just need to begin the array with "and": $criteria = craft()->elements->getCriteria(ElementType::User); $criteria->customNumberField = array('and', '>=5', '<=10'); $...
user avatar
6 votes
Accepted

Ordering of ElementCriteriaModel with varying statuses

Your statuses are incorrect. They would be correct for many elements, but the Entry Element overwrites them to the following: EntryModel::LIVE EntryModel::PENDING EntryModel::EXPIRED BaseElementModel:...
user avatar
6 votes
Accepted

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

You can get your entry's parent entry with getParent(). Use this entry method in a conditional, if your entry has no hero image set. You can nest the code to climb up all of your structure's levels: ...
user avatar
  • 35.8k
6 votes
Accepted

How can I follow relations in ElementCriteriaModels from plugin code?

You're very close! Try this: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'concepts'; $criteria->limit = null; $criteria->relatedTo = array( ...
user avatar
  • 1,818
6 votes

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

Another way to do this would be to assemble a list of the entry IDs that you want to look for a hero image in, in order, and then finding the first hero image related to one of those entries. To ...
user avatar
6 votes
Accepted

Build Query relatedTo multiple categories

Untested, but something like this should work: {# set base criteria #} {% set params = { section: 'productsServices', limit: null } %} {# set relationship criteria #} {% set relationParam = [...
user avatar
6 votes

Display thumbnail using defineTableAttributes for a custom Element Type

Ok, so I've managed to get an answer to this courtesy of Brandon Kelly. It's much more simple than I thought. Firstly, you do not have to specify a relatedTo parameter the defineCriteriaAttributes()....
user avatar
  • 121
6 votes
Accepted

Plugins - Element Criteria Model. Can I return only entries with a updated date in the last 7 days?

I'm not sure if there's some other Craft helpers / static variables that you could use, but this technique works. Create a DateTime object without the param set to get the current time, modify it and ...
user avatar
  • 35.8k
6 votes

ElementCriteriaModel exclude list of ids

This should do the trick: $idList = [1, 2, 3, 4]; $criteria->id = 'and, not ' . implode(', not ', $idList); In plain text, the id property would then look like this: 'and, not 1, not 2, not 3, ...
user avatar
5 votes
Accepted

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

You can do a certain amount of logic in the ElementCriteriaModel syntax. I think this will work: {% set entries = craft.entries.section('equipment').modelYear('and, > 2006, < 2009') %} {% for ...
user avatar
5 votes

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

I think what you want is to check if a property of the asset is empty, rather than the asset itself, as a concept. The following appears to work, and might be of interest if you think you have empty ...
user avatar
  • 1,697
5 votes

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

To get all categories that are related to an entry you would do this: {% set allEntries = craft.entries.limit(null) %} {% set categoriesInUse = craft.categories.relatedTo(allEntries) %} If you want ...
user avatar
  • 35.8k
5 votes

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

I fixed this now by using the method buildElementsQuery, like so: $query = craft()->elements->buildElementsQuery($criteria, $contentTable, $fieldColumns); After that, I was able to add ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible