Hot answers tagged

29 votes
Accepted

Is it possible to exclude multiple sections from an entry search query?

The trick is to add and once before the first not {% set entries = craft.entries.search(query).section('and, not this, not that') %}
user avatar
  • 3,664
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
Accepted

Query only returning 100 records at a time?

Craft imposes a default limit of 100 for element queries. To disable this you need to specify a limit of null. $criteria->limit = null;
user avatar
9 votes
Accepted

Display Entries in a calendar

I haven't tested this, but it should be a good starting point, at the very least. {# Determine the number of days in the month #} {% set daysInMonth = "now"|date("t") %} {# Determine the date range ...
user avatar
7 votes

Will having large numbers of Fields decrease site performance?

I can't answer your question directly as I never got even close to 300 fields yet. But even with 50 fields I had some serious slowdowns, especially when getting related entries in form of assets and ...
user avatar
  • 35.8k
6 votes
Accepted

RelatedTo Query Using Both "and" and "or"

Crisis averted, thanks largely in part to the versitile nature of the relatedTo targetElement processing. The relevation came that I could feed an array of category ids (inclusive) into that element, ...
user avatar
  • 419
6 votes
Accepted

How to get query or param from URL and then use in Links?

Use craft.request.getQueryStringWithoutPath() like so: products/category?{{craft.request.getQueryStringWithoutPath()}} Returns the query string, except for the p= param (which was probably added ...
user avatar
6 votes
Accepted

When is the find() method actually necessary?

The ElementCriteraModel description in the docs explains what the find() method does. Essentially, when you use craft.entries you are building an ElementCriteriaModel object that represents the query ...
user avatar
5 votes
Accepted

How to query using variables

I think you are using the wrong string concatination operator. ~ is the twig string concatination operator (vs . for php and + for javascript). Try this: {% set entries = craft.entries.section('...
user avatar
5 votes
Accepted

How do I update a column value in my custom plugin with a simple controller?

You are currently performing a SELECTinstead of an UPDATE so no data will be updated. I'm guessing that entryId is the primary key of the record you are trying to update? If so, I would suggest you ...
user avatar
  • 1,623
5 votes
Accepted

How can I expand on a normal Craft query to sort by a column in a custom table?

You can't do this using the craft.entries tag in twig. You'll need to create a new function in your plugin that returns the entries in your custom order. Assuming you can create an array of element ...
user avatar
  • 6,146
5 votes

Query only returning 100 records at a time?

By default, Craft sets a limit of 100. Set it to null: limit(null).
user avatar
  • 99
4 votes
Accepted

Get entries by date but ignore time

This happens because you set the parameter on the ElementCriteriaModel to get entries with that specific datetime. If you omit a time when you compose a datetime with strings, you'll still get a time (...
user avatar
  • 35.8k
4 votes
Accepted

How can I use a WHERE statement in my template?

You would first need to query for the users that match your criteria and than query for the profile entries which are related to one of these users. {% set userIds = craft.users({ year: craft....
user avatar
  • 35.8k
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
3 votes
Accepted

Using Services to get all the Categories related to an Entry?

If you have an EntryModel and know the category field's handle, you don't really need a service: $categories = $entry->categoryFieldHandle; In other cases, you can construct an ...
user avatar
3 votes
Accepted

How do I sort a collection of entries from multiple sections by date?

Rather than fetching the entries at first, just grab their IDs using ids() instead of find(): {% set featuredBlogId = craft.entries.section('blogs').limit(1).ids() %} {% set featuredArticleId = craft....
user avatar
3 votes

Using relatedTo, and/or and :empty/:notempty in queries

Please don't laugh, but I thought to myself, let's give this a go in Twig and see where it takes me. It very soon began to become clear, that this will end up in lots of criteria models, db queries ...
user avatar
  • 35.8k
3 votes
Accepted

Find Entries with Multiple Matches in a Single Field

The main problem is your URL isn't formatted correctly. You need brackets after each 'size' parameter to make an array of those values. Otherwise, it only picks up the last value. http://domain.com/...
user avatar
  • 2,563
3 votes

Check if Category is in Query

In Craft 3 you don't have to iterate through the chidlCategories manually, you could use the column filter instead, like this: {% set queryCatSlugs = childCategories.all() | column('slug') %} {% if ...
user avatar
3 votes
Accepted

Check if Category is in Query

I would construct an array of the category slugs outside of your childCategories loop (using twig's merge filter: {% set queryCatSlugs = [] %} {% for category in query %} {% set queryCatSlugs = ...
user avatar
3 votes
Accepted

What's the difference between ElementCriteriaModel and DbCommand?

DbCommand represents a SQL statement to execute against a database. ElementCriteriaModel is a model just like any other model in craft/app/models, but it also happens to make it easy to search for ...
user avatar
  • 66.4k
3 votes

Finding entry by content

It sort of depends on what information you have to identify the entry. If your external service knows the entry id that would be the most direct — otherwise you would have to find the entry by some ...
user avatar
3 votes

How can I reduce the number of queries when including a related item within a for loop

Update: I'll leave the original answer because they're certainly still viable options, this is now probably best solved by using the eager loading features that Craft 2.6 added. A bit difficult to ...
user avatar
  • 66.4k
3 votes
Accepted

How to write query with startdate <= now && enddate >= now?

ElementCriteriaModel parameters always add up to further filter down the results, which means an element has to be truthy for each one of them to be returned. So your params basically look right for ...
user avatar
  • 35.8k
3 votes

Tag mysteriously not found

You don't need to use the search parameter in this case. I'd avoid using it where ever possible. {% set tag = craft.tags.slug(slug).first() %}
user avatar
  • 35.8k
3 votes

Using relatedTo programatically?

You're using $criteria, but attempting to set the relatedTo property on $this->criteria. Assuming that's not the issue, have you tried using $criteria->setAttribute('relatedTo', ['targetElement'...
user avatar
3 votes
Accepted

Correctly using eager loading in Plugin development

Eager loading only works when your field is a RelationField it does not work with Text fields. If you goal is to reduce the length of the queries and to maximize your performance you could fetch the ...
user avatar
3 votes

Show Entries based on content in a Matrix Field

I was finally able to piece this together using Brad's explanation of MatrixBlocks parameters and this thread Can I get an entry by Matrix Block ID? Seems merge([block.owner.id]) was the piece I was ...
user avatar
3 votes
Accepted

Help with an Entries field to an array I can use elsewhere

At the same time as you are iterating over your Hero posts, you could populate an array that you could then use in your most popular query. This is just sudo code but hopefully you get the idea: {# ...
user avatar
  • 7,095

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