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
16 votes
Accepted

How would I include the first category slug in the section's entry URL format?

This got it working for me: /section/{categoryFieldName.first.slug}/{slug}
user avatar
  • 1,661
15 votes
Accepted

How to paginate search results?

{{ paginate.nextUrl }}?q={{ query }} did the trick with this setup.
user avatar
  • 1,128
15 votes
Accepted

Advanced query/filter based on date and values

You're actually closer than you might think. All you need to do is add your field handles as parameters. I prefer to use the hash style of declaring parameters but either way will work, I just find ...
user avatar
  • 2,301
13 votes
Accepted

Search matrix for blocks with upcoming dates

Here’s how you would do that: {% set futureEvents = entry.myMatrixField.myDateField('>= '~now) %} {% for event in futureEvents %} {{ event.description }} {% endfor %} Update: This actually ...
user avatar
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')), ...
user avatar
11 votes

How can I display search "excerpts" from multiple sections?

There is no way at present to delegate a field as the search excerpt field in Craft, so it needs to be handled by the search results template. It is possible to use conditionals but this can bloat the ...
user avatar
  • 6,226
11 votes
Accepted

Display entries if Field Date is past Now

You can combine a for and if statement in twig, so you don't necessarily need to use search. Something like this might get you a bit closer: {% for entry in craft.entries.section('event') if entry....
user avatar
11 votes
Accepted

Select entries starting with same letter from alphabet list

First, you should create a dynamic route (something like "games/glossary/<slug>") and point it to a template. A dynamic route means you won't have to create a template for every letter. Note the ...
user avatar
10 votes
Accepted

Search order "Score" criteria

When Craft is determining a matching element’s score, it will loop through each of the search terms, and for each one, loop through each of the element’s attributes, and add the following points to ...
user avatar
10 votes
Accepted

Using “search” vs. “relatedTo” in a template

To be honest, those two parameters may seem to be slightly similar but provide a totally different functionality. From their documentation on search: search Only fetch entries that match a given ...
user avatar
10 votes
Accepted

Building Results From Advanced Search Form

You can set multiple conditions on a single ElementCriteriaModel parameter. Pass it either a comma-separated list as a string or an array of conditions. {% set entries = craft.entries.id('84, 62, <...
user avatar
  • 35.8k
10 votes
Accepted

How to filter entries by clicking multiple categories on front end

First, I would use something like training in your query string. It's a little more readable and you aren't searching for the words lunge and dumbbell anywhere in those entries, just entries related ...
user avatar
  • 2,573
9 votes
Accepted

How can I display search "excerpts" from multiple sections?

This can be accomplished using a conditional: {% if entry.section == 'sectionA' %} {{ entry.sectionAField }} {% elseif entry.section == 'sectionB' %} {{ entry.sectionBField }} {% endif %} or ...
user avatar
  • 2,455
9 votes

What is the best way to track, record or view search terms?

We track search terms on buildwithcraft.com the same way we track all other site activity: using Google Analytics. All you have to do is throw GA event tracking code into your search results template....
user avatar
9 votes
Accepted

Filter based Search Form

I managed to figure this out by looking through a few other posts: Advanced Search Building a simple search by multiple categories Building Results From Advanced Search Form How to filter entries by ...
user avatar
9 votes
Accepted

How to fetch entries in custom plugin Craft 3?

For instance like this: $entries = Entry::find() ->section('sectionName') ->orderBy('fieldName') ->limit(null) ->all(); Have a look here in the documentation (for the ...
user avatar
  • 839
8 votes
Accepted

Building a simple search by multiple categories

Building the form is pretty straightforward, you get the categories from your "locations" and "types" category groups using craft.categories and loop them to build your select options. I choose to ...
user avatar
  • 35.8k
8 votes
Accepted

Searching a specific section by four different types of parameters?

One method is to build your params before performing the search. {# set base params #} {% set params = { section: 'portfolio', limit: null } %} {% set relatedParams = ['and'] %} {# status #} ...
user avatar
8 votes
Accepted

How do I exclude a Channel, Structure or Singles from search?

This should work for a single section: {% set entries = craft.entries.section('not news').search(query).order('score') %} to exclude more sections: {% set entries = craft.entries.section([ 'and'...
user avatar
  • 35.8k
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 ...
user avatar
  • 1,075
8 votes
Accepted

Craft 3 Multi Category and Field Search

Solved this by merging the search parameters together for both the multiple categories and the number fields if they were set in the url parameters. This solves the loop.last pagination problem as ...
user avatar
7 votes
Accepted

How would you recommend integrating craft with Algolia

Disclaimer : I'm the dev of the SearchPlus plugin The SearchPlus plugin by SquareBit (currently in private beta) integrates directly with Algolia and handles all the index population and updates. ...
user avatar
6 votes
Accepted

Bug with non-ascii characters, quotes and OR operator in search?

This looks like a bug to me too. Here are the search terms and generated queries: 'pølser OR pålegg' - SELECT * FROM `craft_searchindex` WHERE (MATCH(`keywords`) AGAINST('pølser palegg' IN BOOLEAN ...
user avatar
6 votes
Accepted

How to search by title field only?

This seems to work: {% set entries = craft.entries.section('not lakeDetails').search('title:' ~ query).order('score') %}
user avatar
  • 1,106
6 votes
Accepted

Paginate an array with multiple filters

Try using this (completely untested): {% if paginate.prevUrl %} <a href="{{ paginate.prevUrl }}?{{ craft.request.getQueryStringWithoutPath() }}">Previous Page</a> {% endif %} {% if ...
user avatar
  • 66.4k
6 votes
Accepted

Search Results In a Drop List/Popup on Same Page

You can request any template via ajax using jQuery. jQuery would essentially listen for onChange events from the search field and ajax load a template that looped through the search results. The ...
user avatar
6 votes
Accepted

How do I get extracts in search results?

I think creating your own Twig filter is your best bet. So, create a filter that takes a string, searches that for another string and returns an extract. It could do this by using some regular ...
user avatar
  • 6,288
6 votes

multiple search

Your search syntax of craft.entries.section('pages').search('foo:bar foo2:bar2') should work and is the equivalent of saying "give me all of the entries in the pages section where the field foo is ...
user avatar
  • 66.4k
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 ...
user avatar
  • 161

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