Hot answers tagged

29

The trick is to add and once before the first not {% set entries = craft.entries.search(query).section('and, not this, not that') %}


16

This got it working for me: /section/{categoryFieldName.first.slug}/{slug}


15

{{ paginate.nextUrl }}?q={{ query }} did the trick with this setup.


14

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 this easier to read. {% set params = { section : 'events', order : 'eventDate asc', eventDate : '>=' ~ now, eventType : 'green', limit : '10' } %} {...


13

Here’s how you would do that: {% set futureEvents = entry.myMatrixField.myDateField('>= '~now) %} {% for event in futureEvents %} {{ event.description }} {% endfor %} Update: This actually hadn’t worked as I expected to, but it does as of Craft 2.3.


11

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 template, especially when there are multiple sections and each section outputs different fields in different ways, so I prefer to create dedicated search ...


11

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.postDate < now %} {# output your event here #} {% endfor %} It'll just output what meets the criteria if your if statement. The bonus is it doesn't mess ...


11

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 use of the <slug> token, which – although not entirely semantic – should do nicely in this case. In your template, you'll need to pull the letter from ...


10

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 the element’s score: If the attribute is an exact match of the search term, 100 points will be added If the attribute contains a full-word match of the search ...


10

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 search query. (See Searching for the syntax and > available search attributes.) Which means whatever you pass in .search(query), and there is a rather complex ...


10

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, < 7, 29') %} {% set entries = craft.entries.id([84, 62, < 7, 29]) %} In this example, each entry with any of these IDs is returned (→ or logic). To return ...


10

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 to those category slugs. {# First you would find any training categories in the URL query string. #} {% set trainingQuery = craft.request.getParam('training') %}...


9

This can be accomplished using a conditional: {% if entry.section == 'sectionA' %} {{ entry.sectionAField }} {% elseif entry.section == 'sectionB' %} {{ entry.sectionBField }} {% endif %} or a switch statement: {% switch entry.section %} {% case "sectionA" %} {{ entry.sectionAField }} {% case "sectionB" %} {{ entry....


9

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. If you’ve upgraded to Universal Analytics/analytics.js, use this: {% set js %} ga('send', 'event', 'Search', 'Search', '{{ query|e("js") }}', {'...


9

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 clicking multiple categories on front end Fetching entries from an advanced search form on the front end Which I then took their knowledge and put together my ...


9

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 ...


8

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 pass the category's slug as the value to submit and /results as the URL to submit to. <form action="{{ url('results') }}" method="get"> <label>...


8

One method is to build your params before performing the search. {# set base params #} {% set params = { section: 'portfolio', limit: null } %} {% set relatedParams = ['and'] %} {# status #} {% if craft.request.getParam('status') %} {% set status = craft.request.getParam('status') %} {% set params = params|merge({'companyStatus':status}) %} ...


8

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', 'not news', 'not blog' ]).search(query).order('score') %}


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 ...


7

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. There's also a few helpers for integrating on the front-end. All told - you should be able to just drop the plugin onto the site, configure your indexes and ...


7

For instance like this: $entries = Entry::find() ->section('sectionName') ->orderBy('fieldName') ->limit(null) ->all(); Have a look here in the documentation (for the code examples you can toggle Twig and PHP view for code examples): https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html Note that if you add use ...


6

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 MODE)) AND `elementId` IN (3,18,21,22,23,24) '"pølser" OR "pålegg"' - SELECT * FROM `craft_searchindex` WHERE (`keywords` LIKE '% pølser or %') AND MATCH(`...


6

This seems to work: {% set entries = craft.entries.section('not lakeDetails').search('title:' ~ query).order('score') %}


6

Try using this (completely untested): {% if paginate.prevUrl %} <a href="{{ paginate.prevUrl }}?{{ craft.request.getQueryStringWithoutPath() }}">Previous Page</a> {% endif %} {% if paginate.nextUrl %} <a href="{{ paginate.nextUrl }}?{{ craft.request.getQueryStringWithoutPath() }}">Next Page</a> {% endif %} Update: The ...


6

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 template that you request can even be the default full search results page, with a conditional that determines which part of the template gets returned if the ...


6

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 expression hocus pocus, I guess. {{ entry.bodyText | extract('your search term', 3, 3) }} would be how you'd use it (assuming, for example, that your filter accepts ...


6

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 equal to bar and the field foo2 is equal to bar2. Something that trips people up often is that Craft doesn't do fuzzy searching by default. So foo:bar will match ...


6

If you're used to another CMS such as WordPress, you're probably used to using a Discourage search engines from indexing this site option. As Robin's answer mentions, the training wheels are off. However, that doesn't mean you can't replicate WP's functionality. When you tick that box, there's a few things that happen behind the scenes: If robots.txt ...


6

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 well. Here is the code I used: {# set base params #} {% set params = { section: 'vehicles', limit: null } %} {% set relatedParams = ['and'] %} {# add ...


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