Skip to main content
9 votes
Accepted

Craft 3: Get Data from a custom Table via createCommand()

You can use either an activeRecord for it $customer = Customer::find() ->where(['id' => 123]) ->one(); Or a query $rows = (new \craft\db\Query()) ->select(['id', '...
Robin Schambach's user avatar
9 votes

Get array of titles instead of whole collection of entries' attributes

❗️ This solution will be dramatically simplified in Craft 4.3! Unfortunately, the answer differs pretty dramatically for native properties with "stable" column names and for Content, which (...
August Miller's user avatar
8 votes
Accepted

How to query an entry by a custom field in Craft 3

Your where clause is actually a query based where so you have to select the database fields instead of the field handles. When you want to do it with where you have to make it like that $country = ...
Robin Schambach's user avatar
7 votes

Get array of titles instead of whole collection of entries' attributes

Update for Craft 3.7 Craft 3.7 adds a new field property columnSuffix which is way easier to use than my example code below. Now you can simply access any field's column suffix in Twig like this: {% ...
MoritzLost's user avatar
  • 11.7k
6 votes
Accepted

Query entries between two values

I got the answer to this thanks to @BrandonKelly from a comment in this question: How can you return elements between two values? Turning it into a subarray does the trick: expirationDate: [ 'and', '&...
Steven Thate's user avatar
6 votes

Most efficient way to check if a section has any active entries

You can use {{ craft.entries.section('whatever').total() }} for that.
Fred Carlsen's user avatar
  • 2,997
5 votes
Accepted

List Only Available Times/Dates in Solspace Calendar

Edited to provide a cleaner solution: This code will display different contents for available times and unavailable times in 30 min increments, and makes no assumption on how long your events are. {%...
Solspace's user avatar
  • 1,410
5 votes
Accepted

Query entries by custom date field (for a particular year)

I think your problem is you're converting the date to a year string then converting that to an atom value. Just convert the date straight to an atom value: {% set yearStart = "#{year}-01-01"|atom %} {...
kmgdev's user avatar
  • 1,199
5 votes

Element query for specific fields

@MoritzLost's answer should be the accepted one. But, as an alternative approach you could also use the |map Twig filter to "prune" the entry objects returned from a normal element query: To ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

How to get column name of an entrie's field

It's actually OK to hard code the complete field column name (i.e. field_nationality_gkpaccfs) – it won't change between environments, or if the field is resaved (unless you change the handle, of ...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Searching for any keyword in query

One way to do it is to split the search query on spaces, and then join it with 'OR': {% set query = craft.request.getParam('q')|split(' ')|join(' OR ') %} {% set entries = craft.entries.search(query)....
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Most efficient way to check if a section has any active entries

Not a definitive answer at all, but you could at least add a .ids() on that so it only retrieves the entry ids and not all the entry data... {% if craft.entries.section('whatever').ids() | length >...
Jeremy Daalder's user avatar
4 votes
Accepted

Where should the .all() be placed in Craft 3

Both approaches are valid. In your first example, you could have moved .all() into the for-loop; and in the second example, you could have moved .all() out of the for-loop, and into the {% set entries ...
Brandon Kelly's user avatar
4 votes
Accepted

How to query entries by field values

There is a related to parameter you can use Entry::find() ->section($section) ->relatedTo(['targetElement' => $image->id, 'field' => 'galleryImages') ->all();
Robin Schambach's user avatar
4 votes
Accepted

How to query entries written in a specific locale? (Craft 3)

In Craft 3, you can't really have locales. If in Craft 2 you had multiple locales, that should have been automatically migrated to multiple sites (as now the site is always single locale) Having said ...
Yuri Salimovskiy's user avatar
4 votes
Accepted

Excluding specific field value from search

The light switch value should be taken out of the search query and put into the entry query: {% set query = 'title:"' ~ "myValue" ~ '"' %} {% set results = craft.entries({ section: querySections, ...
dmatthams's user avatar
  • 2,502
4 votes
Accepted

Query entries posted in the past month

Immediately found a solution! {% set newsQuery = craft.entries({ section: 'news', after: now|date_modify('0:00 -1 months'), }) %}
supazu's user avatar
  • 526
4 votes
Accepted

Get entries with a specific post date

You’d need to define a range that begins today and ends tomorrow. {% set startDate = startDate ?? now %} {% set endDate = startDate|date_modify('+1 day') %} {% set entries = craft.entries() ....
Brandon Kelly's user avatar
3 votes

How to query an entry by a custom field in Craft 3

I might be missing some important SQL-ism, here, but I think the question has a much simpler answer. The ElementQuery docs bury it a bit, and I wonder if many new Craft developers have trouble ...
August Miller's user avatar
3 votes

Merge two queries of entries ordered by publication date

You can probably use the relatedTo parameter with multiple relation criteria: {% set entries = craft.entries.section(['news', 'press']).relatedTo('or', { targetElement: newsTypeCategory, field: '...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Difference between chained and batch element queries?

general The first way creates an object of type EntryQuery, you then call the 2 functions section and limit In PHP you would do the following: $query = Entry::find()->section('news')->limit(...
Robin Schambach's user avatar
3 votes
Accepted

Craft query Matrix blocks in twig

Yes you can. craft.entries returns an object of type EntryQuery There is a function matrixBlocks in the CraftVariable that returns an object of type MatrixBlockQuery Both of them have nearly the ...
Robin Schambach's user avatar
3 votes

Element Query matrixfields where

You need to join the matrixblocks and the specific content table $query->join('{{%matrixblocks}}, 'matrixblocks.ownerId = elements.id); $query->join('{{%matrixcontent_your_table_name}}, '...
Robin Schambach's user avatar
3 votes

Element Query matrixfields where

When joining the tables you get more results like you mentioned in Edit2. First pick distinct ids, to use later on in the main query. I came up with this: $timeSlotIds = Entry::find() ->...
Jeroen's user avatar
  • 308
3 votes

How do multiple date queries differ between Craft 2 and 3

My Craft 3 version is: {% set result = craft.entries() .section('events') .eventDate( ['and', '<' ~ now|atom, '>' ~ now|date_modify('-6 days')|atom ] ) .results(':notempty:') ....
Paul Frost's user avatar
  • 1,060
3 votes
Accepted

Where to put where() for a query looking for a lightswitch field

You can just query for the field value directly How to use Element Queries {% set featuredEntry = craft.entries({ section: ['resources'], orderBy: {postDate: SORT_DESC}, limit: 1, ...
Robin Schambach's user avatar
3 votes

Showing entries for light switch

Try rewriting your element query like so... craft.entries .section(['etiquette','style','culture']) .requiredReading(true) .orderBy('id desc') .limit(12) .all() In Craft you can do queries against ...
Mutual's user avatar
  • 442
3 votes

Returning entries that contain both categories

Answer for Craft 2 Your final result should not look like relatedTo('and', '1,2') but relatedTo('and, 1, 2') Or relatedTo(['and', 1, 2]) So you just need to do relatedTo('and,' ~ param) Keep in ...
Robin Schambach's user avatar
3 votes

Returning entries that contain both categories

This should do it: {% set eventCats = craft.request.getParam('cat')|split(',') %} {% set events = craft.entries.relatedTo(['and']|merge(eventCats)).find() %}
Mats Mikkel Rummelhoff's user avatar
3 votes

How to get previous and next entry using the native GraphQL API?

So I'm wondering the same thing, I'm using CraftQL as I need to support mutations, and I am close with this - but I get an error if I try to use $id instead of a hard-wired Int (e.g. 99): query ...
littlered's user avatar
  • 151

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