12 votes

Filter Entries by Field Value

Filtering a list of entries is quite simple. Here's an example from one of our sites: {% for job in craft.entries({ section: 'jobs', applicationsClose: ">= " ~ (now|date('Y-m-d')), ...
10 votes
Accepted

How to order or filter entries by their author's custom field?

This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query. We first perform an inner join on the author's content table using the authorId so that the ...
  • 6,401
8 votes
Accepted

Filter Entries by Field Value

Use the search parameter in the craft.entries model: {% for entry in craft.entries.section('Blog').search('featuredFieldName:1') %} {# Entry Code #} {% endfor %} To filter by a specific text ...
  • 1,075
6 votes

Filter Entries by Field Value

{% set featuredProducts = craft.entries .section('sectionName') .featured(true) .all() %} Where sectionName is the name of your section; featured is your featured field handle; true is ...
  • 161
3 votes

orderBy title, but remove the word "The" from titles

{% set entries = craft.entries().section('media').orderBy("(CASE WHEN `title` LIKE 'The %' THEN SUBSTRING(`title`, 5) WHEN `title` LIKE 'A %' THEN SUBSTRING(`title`, 3) ELSE `title` END) asc"...
  • 85
3 votes

Filter Entries by Field Value

Another way for Craft 3 {# Create an entry query with the 'section' and 'limit' parameters #} {% set entryQuery = craft.entries() .section('Blog') .search('entryColor:green') .limit(10) %} ...
  • 4,696
3 votes
Accepted

Only show entries related to category

Personally I would set the category select field outside of the matrix block but that's up to you. The problem you are running in to at the moment is you are not looping over the matrix field. {% ...
2 votes
Accepted

Filter Entries by Category fields & refresh URL and list with AJAX

First you need some HTML/Twig logic to display your categories <div> {% for category in categories %} <input data-id="{{ category.id }}" data-slug="{{ category.slug }}" type="...
2 votes

How to filter entries by tags array using ['or'] and by category not using or

The relatedTo function accepts multiple different formats. If you want to search for Elements that are related to a list of categories and a list of tags you can do that like this relatedTo([ '...
2 votes
Accepted

Product Filtering with Tags

Not sure i understand well, but to be sure: you want to be able to filter for (let's say you sell shirts) a Red AND Large shirt, so it filters only the shirts which contain the color red and that are ...
2 votes
Accepted

filter matrix blocks by multiple light switch value, and then merge them

Keep in mind objects in PHP are always called by reference not by value thus your anchors query will always return blocks from the type subhead with the field value anchor =1 while your matrix query ...
2 votes

Filter entries by Field value using a variable

Twig has an interpolation syntax (using #{} inside a double-quoted string) to accomplish this: {% set location = block.locationVacancies %} {% set entries = craft .entries .section('careers') ...
2 votes

Sort entries by time part only of DateTime object

For those interested, I found out you can tell Craft to search the database for the time part only with the mySQL command time() (Order entries by custom time field). For example: {% set shows = ...
  • 61
2 votes

How to create a faceted search/facet filter in Craft Commerce

Answer edited 2022: I've now written a blog post tutorial on how to do multi-faceted search filtering natively: https://cld.agency/blog/multi-faceted-filtering-in-craftcms I've done this a few times ...
  • 4,059
2 votes

How to create a faceted search/facet filter in Craft Commerce

Algolia is excellent, and I've found the scout plugin to be solid. Algolia will give you pre-built faceted navigation with instantsearch.js so there is very little development time. Elastic search ...
  • 2,462
2 votes
Accepted

Filtering entires via tag field and checkbox field using a form

I'm not sure why your search is not working in this instance (though it might be because you're not executing the query, see my comment). But in general, fulltext search is way less precise, I would ...
  • 6,925
2 votes

Filtering lists of entries in the control panel by entry type

Out-of-the box, Craft doesn't offer such filters, but adding them isn't that hard using a module. You need to listen to 2 events: \craft\elements\Entry::EVENT_REGISTER_SEARCHABLE_ATTRIBUTES: to add ...
  • 1,130
1 vote

Craft Commerce: Filter completed orders based on a product category field

Managed to solve this. First, I've added a categories field to the order form. On the checkout page I loop over all lineItems in the cart and collect the category id's like this. {% set categoryIds = [...
1 vote
Accepted

Using a Matrix field to create a homepage that shows entries from sections and categories

I figured it out. I changed {% set category = entry.slug %} {% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %} to {% set category = craft.categories.slug(entry....
  • 85
1 vote

Order grouped results by dropdown order

You could wrap this in another for loop and put a condition on the inner fors, something like {% for option in myPositionDropdown %} <h2>{{ option.value }}</h2> {% for position, ...
1 vote

How to use checkboxes to filter categories on the front end?

Here is how I did it at my end for the templating side of things: Set the queries you need and merge them into arrays like below. Please be aware this was done for craft commerce 2 products but the ...
1 vote

Arrow functions in twig or craft cms

If you want to use arrow functions everywhere, so you can assign them to variables for re-use, or pass them to Collection methods that support callbacks, you'll need Craft Closure: https://github.com/...
  • 11.1k
1 vote

Arrow functions in twig or craft cms

Twig didn’t add arrow function support until v2.10.0, so you need to be on at least Craft 3.1.29 if you want to use them.
1 vote

Sort entries by time part only of DateTime object

If your day field is a relation field you should search for your related entries with {% set today = insertYourLogicToFetchTheCurrentDay %} {# eg set todayName = "now"|date("l")#} {# set today = ...
1 vote

Pass 'not' category.id to relatedTo in Craft

You need to exclude the ids in order to achieve your goal. So instead of relatedTo not you need a normal related to but the not statement in your id I'm not on my computer currently so I'll edit the ...
1 vote

How can I filter Craft Commerce products by the current price

https://craftcommerce.com/docs/variant-model#saleprice The comment on the documentation leads me to believe you can just use salePrice for your filtering as it will be equal to the defaultPrice if ...
  • 305
1 vote

Filter entries by Field value using a variable

{% set location = block.locationVacancies %} {% set entries = craft .entries .section('careers') .search('locationVacancies:"' ~ location ~ '"') .find() %}
1 vote

Filtering by categories AND tags with full match

So it turns out there were a few things wrong. Firstly my tag slugs were wrong and didn't match in the database (Craft needs to improve how it handles tags...) Secondly, I had to change my code as ...
  • 21
1 vote

Filtering by categories AND tags with full match

You are always one level too deep. Craft is able to proceed parameters like [ 'and', ['targetElement' => [1, 2, 3], ['targetElement' => ['or', 1, 2, 3], ['targetElement' => 1]...
1 vote
Accepted

How can I filter Craft Commerce products by price

Looks like defaultPrice is the property on the Commerce_ProductElementType class you can use for this: i.e. {% set queryParams = { relatedTo: category, defaultPrice: [ 'and', ...
  • 66.5k

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