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’...
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 ...
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 ...
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) ?: ...
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 ...
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: ...
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, ...
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 ...
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 ...
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.
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 ...
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, ...
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 ...
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 ...
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 }...
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 ...
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), ...
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 ...
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....
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 ...
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 $...
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::...
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 = '...
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 ...
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
...
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 (...
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(
'...
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 ...
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 ...
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:';
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
elementcriteriamodel × 187plugin-development × 65
entries × 21
plugin-elementapi × 19
templating × 16
relations × 13
elementtype × 12
matrix × 10
search × 10
assets × 9
categories × 8
eager-loading × 8
craft3 × 7
database × 7
pagination × 6
plugin-craftcommerce × 5
datetime × 5
performance × 5
queries × 5
localization × 4
php × 4
query × 4
array × 4
plugin-smartmap × 4
element × 4