11

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')), internalExternal: 'external', }) %} ... {% endfor %} Here we define the section we're looking in and then filter by two custom fields: applicationsClose and ...


9

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 custom field is available to us. in Twig: {% set elementQuery = craft.entries() .section('sectionHandle') .with(['author']) .innerJoin('{{%content}} ...


8

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 snippet: {% for entry in craft.entries.section('Blog').search('entryColor:"green"') %} {# Entry Code #} {% endfor %} You can create more complex search ...


5

{% 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 the value you want to query the lightswitch fields for. Each field type in the Craft docs has an element query example, e.g. lightswitch fields


3

{% 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").limit(24).all() %} worked.


3

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) %} {# Fetch the entries. #} {% set entries = entryQuery.all() %} {% if entries|length > 0 %} {% for entry in entries %} {{ entry....


3

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. {% for entry in craft.entries.section('job') %} {% for block in entry.matrixFieldName if block.type == 'job' %} {% for category in block.category %} ...


2

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="checkbox" class="category-filter">{{ category.title }} {% endfor %} <a href="" id="submit">Submit</a> </div> Then some Javascript ...


2

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([ 'and', ['element' => [1, 2, 3]], ['element' => [4, 5, 6]] ]); This will return you all Elements that are related to the first array and related to ...


2

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 available in large? On that note, have you tried setting relatedToParam to and instead of or? The answer you linked seems to work for multiple selections that ...


2

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 anchorLinks will return blocks of type copy with the field value anchor = 1 and anchorLink = 1 I'm not sure why you don't fetch the results in one single query ...


2

Twig has an interpolation syntax (using #{} inside a double-quoted string) to accomplish this: {% set location = block.locationVacancies %} {% set entries = craft .entries .section('careers') .search("locationVacancies:#{location}") .all() %} In addition, you can be more explicit about the values you’re searching for (and avoid any kind of ...


2

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 = craft.entries({ section: 'shows', relatedTo: day.id, order: "time(startTime)", }) %} However, this sorts based on the absolute DB time (GMT time), so I ...


2

I've done this a couple of times in Twig and a couple of times with the Faceted Nav plugin and would recommend doing it yourself in Twig, as it gives you full control. More specifically, Faceted Nav plugin cannot achieve what most people want which is to separate the filter groups with 'AND' logic while separating the individual filters in each group with '...


2

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 comes with the added complexity of running/configuring an ES server, will likely require on-going maintenance. However if you have the time and don't want to ...


2

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 only do that if you need to support actual search terms. Since you're only filtering by categories and tags, I wouldn't search by category/tag titles, but ...


1

You can use either query parameters or a second URL segment to have both the store and the country available for filtereing. For example, let's say your base URL for a store page is something like this: example.com/barnesnoble/. You can set your links for countries to either of those: example.com/barnesnoble/us example.com/barnesnoble/?country=us Then in ...


1

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 = [] %} {% for item in cart.lineItems %} {% set categories = item.purchasable.product.productGroup.leaves().all()%} {% for category in categories %} {% ...


1

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.slug) %} {% set entries = craft.entries.section(section).relatedTo(category).limit(4).orderby('postDate desc').all() %} and it shows up as expected.


1

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, playersInPosition in playersGrouped if position == option.value %} <ul> {% for player in playersInPosition %} {% set playerData = craft....


1

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 same can be applied to entries too as it is the same logic. {% set types = craft.categories.group('flowerTypes') %} {% set sizes = craft....


1

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

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 = craft.categories.title(todayName).first() #} {% set showsToday = craft.entries.section('shows').relatedTo(today).find() %} I don't really understand the second ...


1

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 exact code in about 2h but it's the same like that one https://craftcms.stackexchange.com/a/22532/5557 id.('and, not ' ~ idsOfEntriesYouWantToExclude|join(', ...


1

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 there is no sale. So: {% set queryParams = { relatedTo: category, salePrice: [ 'and', '>= ' ~ 0, '<= ' ...


1

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


1

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 Robin suggested to .one() instead of grabbing the full array. Thirdly, I had to change my targetElement to 'or' for each array merge then 'and' for the final ...


1

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], ['targetElement' => ['and', 1, 2, 3] ] And so on. Yours is 'targetElement' => ['and', [1,2,3]] I would suggest you to dump your array <pre&...


1

Looks like defaultPrice is the property on the Commerce_ProductElementType class you can use for this: i.e. {% set queryParams = { relatedTo: category, defaultPrice: [ 'and', '>= ' ~ 200, '<= ' ~ 500, ] } %}


1

Ideally, you'll use a dynamic route that can match any number of tags – negating the need to hard code the number of possible tag segments. You can't pull this off using the Routes admin inside the CP, so crack open your /craft/config/routes.php file and add this: return array( 'tags/(.+)' => 'path_to_your_tags_template', ); The above route can ...


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