Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

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', '...
user avatar
9 votes
Accepted

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

Unfortunately, the answer differs pretty dramatically for native properties with "stable" column names and for Content, which (as of Craft 3.7.0) now contain a "suffix" that (in ...
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 = ...
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.
user avatar
  • 2,947
6 votes

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

Just to expand on the caveat regarding field suffixes in Craft 3.7+, I asked on Github what the recommended way to include field suffixes in a custom query is. Here's brandonkelly's response: The ...
user avatar
  • 6,070
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. {%...
user avatar
  • 1,390
5 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', '&...
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 >...
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 ...
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();
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 ...
user avatar
4 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 %} {...
user avatar
  • 1,075
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, ...
user avatar
  • 2,437
4 votes

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'), }) %}
user avatar
  • 337
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: '...
user avatar
3 votes
Accepted

Optimizing pagespeed

Not sure how you implemented the cache tags, but I would recommend to cache each "eDrops" entry individually. I also took out {% if entry.slides|length %}. Testing if entry.slides.first() returns ...
user avatar
  • 35.8k
3 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)....
user avatar
3 votes
Accepted

How to Check All Users against UserGroup

Got it all sorted: {% if craft.users.group('myusergroup').total() > 0 %}
user avatar
3 votes
Accepted

Searching multiple queries using CraftCMS search function

You'd have to set up an ElementCriteriaModel with parameters adapted to your entry type and the values you're getting from the query string. I'd recommend to read the official docs on that topic ...
user avatar
  • 35.8k
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 ...
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(...
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}}, '...
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() ->...
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:') ....
user avatar
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, ...
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 ...
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 ...
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() %}
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 ...
user avatar
3 votes
Accepted

Offset setting a limit?

When you're setting section.links.limit(4), Craft will persist that limit in subsequent calls. This was a change introduced in Craft 3, which you can read about Changes in Craft 3 | Cloning Element ...
user avatar
  • 190

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