Skip to main content
12 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 ...
Ben Croker's user avatar
  • 7,631
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....
Steve Holland's user avatar
10 votes
Accepted

How to load entries via ajax when a filter is clicked?

There's a ton of ways to pull this off, but in my opinion setting up pages/URLs for each category is probably a good idea, for SEO and sharing reasons. It's not something that requires a lot of work ...
Mats Mikkel Rummelhoff's user avatar
10 votes
Accepted

Alphabetical index for a list of entries

First, sort all entries by multiple criteria: {# get our entries and order by firstname, lastname #} {% set allEntries = craft.entries.section('entries').orderBy('lastname asc, firstname asc').all() %...
Urs's user avatar
  • 639
7 votes
Accepted

Filtering multiple categories with URL queries

The issue with your current code is that you're looping over each type/size category, creating a parameter in your relatedTo query for each one. This means that you're currently creating a query that ...
Mats Mikkel Rummelhoff's user avatar
6 votes
Accepted

How to pull the first block of its kind from a Matrix?

You could try something like this: {% set block = post.buildStory.type('textEditorBlock').first() %} {{block.textEditor}}
alexr's user avatar
  • 628
5 votes
Accepted

Count all unique values

Something like this should do the trick: {% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %} <ul> {% for country, users in usersGroupedByCountry %} ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

How to filter entries based on Tags

You can combine multiple relationship criteria by passing an array to the relatedTo parameter with the keyword 'and' or 'or' as the first array element. You want your entries to be related to any of ...
carlcs's user avatar
  • 36.3k
4 votes
Accepted

Reusing logic to set string variables

I would use macros. A Twig macro is basically a function, that can take arguments and render HTML. Macros can be defined inline (i.e. in the same template where it's used) or in a separate file (...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Group by day does strange exeptions

When you specifically group on dateField.day (in your case, the |group('datum.day') part), the group filter won't care about the month or year for the date field โ€“ only the day. This means that any ...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Using user-selectable timezones to show localized post dates

It is possible to work around the Craft 2 bug, by converting the Craft DateTime object to a native one. {% set craftDateTime = entry.postDate %} {% set nativeDateTime = date(craftDateTime|date('c')) %...
carlcs's user avatar
  • 36.3k
4 votes
Accepted

How can I let the user activate a particular SuperSort filter

So in order to do this you have to reformat your code and your strategy a bit. You cant provide a Craft query as a link, all that does is grab an array from the database and it won't inherently print ...
Alan Miller's user avatar
3 votes

Display entries if Field Date is past Now

Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object. so instead of: {% set futureEntries = craft.entries.section('...
ToddPadwick's user avatar
3 votes
Accepted

Filter syntax for Dimensions & Weight?

There is currently no filter but you can access the units of measurement from your settings, and display the measurement: {% set weightUnits = craft.commerce.settings.weightUnits %} {% set ...
Luke Holder's user avatar
  • 6,837
3 votes
Accepted

Order by fields after searching

Not really feasible without a plugin โ€“ fortunately, there's the excellent SuperSort: {% set results = craft.entries.section('player').search('team:"thenameoftheteam"') %} {% set ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Convert characters to Ascii

I would use a combination of things: Twig's replace filter to convert รค to ae etc. Twig's convert_encoding filter to eliminate any remaining non-ascii characters Craft's kebab filter to do its thing ...
Marion Newlevant's user avatar
3 votes
Accepted

How to output a Rich Text Link to an Entry as a tooltip?

Not really a craft solution โ€” but easiest might be jquery/ajax. Since you know the base URI, you can just create a selector for the route, and work from there. $("a[href*='route/to/definitions/']")....
Douglas McDonald's user avatar
3 votes

How I can sort my entry that belong to 3 section on date?

Try chaining the sections together like: {% set allEntries = craft.entries({ section: ['blog', 'news', 'caseStudies'] }).orderBy('elements.dateCreated asc').all() %} Then {% for entry in ...
Jamie Jenkins's user avatar
3 votes
Accepted

How do Twig filters interact with concatenated strings and shorthand "if-else" ( `var ? true : false` ) strings?

You are asking about Twig's operator precedence: The operator precedence is as follows, with the lowest-precedence operators listed first: ?: (ternary operator), b-and, b-xor, b-or, or, and, ==, !=, &...
Marion Newlevant's user avatar
3 votes
Accepted

Element API: Get Entries Related to Category with Query Parameter

Are you passing the projectCategory parameter as a string or ID? In any case, the relatedTo parameter will receive a string in your code, which might trip up the query. If the parameter is passed as ...
MoritzLost's user avatar
  • 11.6k
2 votes

Twig filter to convert special characters to URL friendly characters

My first idea would be to use the url_encode filter than would turn your special characters into a URL friendly format. I imagine these would also be Javascript friendly. If you'd like to convert the ...
Luke Pearce's user avatar
  • 3,863
2 votes

How to load entries via ajax when a filter is clicked?

I used the accepted answer here as a starting-off point, and I came up with this solution. I know this looks a little convoluted, but it's actually quite minimal given the the size of the site I'm ...
InanisAtheos's user avatar
2 votes

Google AMP Filter

You could use the replace filter, it accepts regex: https://craftcms.com/docs/templating/filters#replace
andrew.welch's user avatar
  • 11.6k
2 votes

Filter products by variants' tags

Personally I would use categories for a 'facetted search' filter you describe, as categories are less likely to get out of control. e.g. If tags are seldom re-used then a search for a tag may only ...
Matt P's user avatar
  • 1,548
2 votes

Set Twig variable with javascript/jquery for displaying entries based on clicked value

The Twig code to fetch entries occurs on page load - Your JS code will run after the page loads and won't be able to update/fetch new entries. Instead, you should look at building a system that uses ...
Luke Pearce's user avatar
  • 3,863
2 votes
Accepted

How to calculate average age members

{# Get all members #} {% set members = craft.users().limit(null) %} {# Get total number of members #} {% set memberCount = members.total() %} {# Set this outside the 'for' loop so you can use it ...
Philip Jones's user avatar
2 votes

How to calculate average age members

I don't think this will exactly answer your question, but I think it will help get you going in the right direction. Within my application, I am grabbing entries that belong to the currently logged ...
Damon's user avatar
  • 4,706
2 votes
Accepted

How to shorten 'craft.request.getUrl()' with the shortener plugin?

The issue was that the custom short name I was using (in this case the development domain) was not associated with the bitly account. Total oversight! I've removed that and it works perfectly.
ByteMyPixel's user avatar
2 votes

Using user-selectable timezones to show localized post dates

This is a known Craft 2 bug in our overridden implementation of PHP's DateTime class and how Twig interacts with it. It should be fixed before the Craft 3 beta launch because it is on our list to ...
Brad Bell's user avatar
  • 67.5k
2 votes

How to filter entries based on Tags

I think what you are trying to achieve here has little to do with the actual templating itself. You can add your tags as data-attributes like so {% set entries = craft.entries.section('yoursection') %...
Jan_dh's user avatar
  • 858

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