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’...
carlcs's user avatar
  • 36.1k
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 ...
Brad Bell's user avatar
  • 67k
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 ...
carlcs's user avatar
  • 36.1k
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) ?: ...
carlcs's user avatar
  • 36.1k
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 ...
jthomas's user avatar
  • 373
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: ...
megatrond's user avatar
  • 1,342
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, ...
Mats Mikkel Rummelhoff's user avatar
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 ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

Why does a Element Criteria Model Query with Strings on a custom element type not filtering?

Found the answer, simple mistake. if ($criteria->code) { $query->andWhere(DbHelper::parseParam('trips.code', $criteria->code, $query->params)); } was needed along with public ...
Dale Inverarity's user avatar
5 votes

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

In Craft 3 you can do this without a plugin: {% set hours = craft.entries .section('channelTimetracking') .sum('field_myCustomField') ?? null %} Details in the docs.
Ottó Radics's user avatar
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 ...
24creative's user avatar
5 votes
Accepted

Add custom element criteria attribute without a field type?

Currently, yes, the only way to modify an element query (when it’s not your own element type) is to do it with a custom field type, from its modifyElementsQuery() function. UPDATE As of Craft 2.5, ...
Brandon Kelly's user avatar
5 votes
Accepted

Modify ElementCriteria and use it for paginate

You can only modify a craft.entries criteria model with a field (→ Add custom element criteria attribute without a field type?). So what you have to do is to create a new custom field type with your ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

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

When I understand Brandon correctly in this answer Query for blocks across Block Types using the new field parameter?, it's not possible to set up what you want with a criteria model. What you can do ...
carlcs's user avatar
  • 36.1k
5 votes
Accepted

A more economical method of building an archive menu?

If you know which years should appear, and there are no breaks in the timeline, perhaps it would make more sense to do a simple numeric loop instead... {% for year in (now.year)..2009 %} {{ year }...
Lindsey D's user avatar
  • 23.8k
5 votes
Accepted

Performance when loading lots of Elements but only need limited data

Not in Craft 2 without completely bypassing ElementCriteriaModel and building your own SQL query. In Craft 3 (currently in beta) you can, though – you can override which columns get selected in your ...
Brandon Kelly's user avatar
5 votes
Accepted

Pagination and Entry Model on Homepage

project.images is an array because you are eager-loading the images. Since you’re only loading one project entry, you’re not actually gaining anything by eager-loading the images (or category), ...
Brandon Kelly's user avatar
5 votes
Accepted

.first() vs. .one() in craft 3

.all() returns a collection of elements, which you can loop through with a for loop. .one() returns a single element, so you cannot use a for loop to access it. In this case you should just store ...
Jeremy Gimbel's user avatar
4 votes
Accepted

Get entry's from fixed ID array

Turns out... the ID's are wrong. I've listed the shortlist ID's and not the entry ID's. Should use entry.elementId. Here's the fix: {% for entry in list.items %} {% set favId = entry....
Johannes Lamers's user avatar
4 votes

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

My SuperSort Plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value. To ...
Michael Rog's user avatar
  • 3,400
4 votes

Is there a way to return asset data along with entry data?

Yes - you can access it exactly the same way you do in the templates; you just need to use PHP syntax (e.g. -> for accessing sub-properties rather than .). 'transformer' => function(EntryModel $...
Brandon Kelly's user avatar
4 votes
Accepted

Get entries by date range from plugin

Ok, found an answer that made it a bit clearer: How do I get elements using getCriteria from a specific date? Basically you can do this: $criteria = craft()->elements->getCriteria(ElementType::...
ladrua's user avatar
  • 283
4 votes
Accepted

Smart Map - Sorting for closest location in PHP

You're very close! $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->myFieldHandle = array( 'target' => 90210, 'range' => 100 ); $criteria->order = '...
Lindsey D's user avatar
  • 23.8k
4 votes
Accepted

Excluding a single Entry ID of an Entry Type from craft.entries

You'd have to add a conditional and render the featured entry on the first paginated page only. You can access that info from the pageInfo variable. Another thing I'd do is to remove the featured ...
carlcs's user avatar
  • 36.1k
4 votes
Accepted

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

Craft 3 Thanks to the where() function this has become easy in Craft 3. {% set query = craft.entries.where([ 'or', {'field_lightswitchA': 1}, {'field_lightswitchB': 1}, ]) %} Craft 2 ...
carlcs's user avatar
  • 36.1k
4 votes
Accepted

Query all categories once and get individual categories from array

Your issue is that as soon as you call .find(), your ElementCriteriaModel returns an array of elements. In other words, the ElementCriteriaModel is gone, and it's no longer possible to add parameters (...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Craft Commerce update variant field in plugin

This is the general mechanism for updating custom fields on elements - use setContentFromPost and then call the element's save function. $variant->setContentFromPost(array( '...
Jeremy Daalder's user avatar
4 votes
Accepted

Filter entries in the CP based on custom permission

After some digging I came up with some answers myself. For future reference, here they are: Is the event triggered by the CP: if(craft()->request->isCpRequest()){} Check the logged in user's ...
Tom De Smet's user avatar
  • 1,456
4 votes
Accepted

Figuring out why my image is not working in the template

You can have more than one Asset in any Assets field, so it always returns to you an array of assets. So you just need: {{ entry.hoofdafbeelding.first().getUrl('square') }} ...assuming you want the ...
andrew.welch's user avatar
  • 11.3k
4 votes
Accepted

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

Splitting the criteria up like this works. $criteria->relatedTo = [ 'targetElement' => anElementId, 'field' => 'relationshipOne' ]; $criteria->relationshipTwo = ':empty:'; ...
Benjamin Kohl's user avatar

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