Skip to main content
10 votes
Accepted

Order by score and another field

I assume you're using .search() – a common mistake is to try to combine orderBy('score') with custom fields, which doesn't work because the score attribute isn't an actual field, but a computed value ...
Mats Mikkel Rummelhoff's user avatar
10 votes
Accepted

URI for nested categories in Craft 3

You can actually use a bit of Twig logic to determine your category URI formats. Try this for your Category URI Format: {parent ? parent.uri : 'what-we-do'}/{slug} Which is just a more compact way ...
Alex Roper's user avatar
  • 2,632
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 ...
Chattervast's user avatar
8 votes
Accepted

Get related posts excluding current post in Craft 3

To exclude the current post use the current post's id with a "not" clause in the id query param. {% for relatedPost in craft.entries.relatedTo(entry.newsCategory).id('not ' ~ entry.id).limit(5).all() ...
a-am's user avatar
  • 2,857
7 votes
Accepted

What is the difference between 'nav' and 'for' for looping?

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in ...
Lindsey D's user avatar
  • 24k
7 votes
Accepted

Ordering of products?

Product types are really all about data concerns - so if the data you're storing for your canines/equines (hereafter known as dogs and horses to save some typing ;) ) - is the same, then you're right ...
Jeremy Daalder's user avatar
7 votes
Accepted

Custom Category order in a Categories Field

No, it’s not possible to change the order of related categories. (Think of the field as a big checkbox list; you can change which checkboxes are checked, but you can’t change their order. The only ...
Brandon Kelly's user avatar
7 votes
Accepted

Need .last() functionality for categories – .last() will be deprecated going forward

If you are not going to update your current project to Craft 3 (which will break nearly all your templates and all plugins anyway) you don't need to bother about that message. This is only for Craft 3 ...
Robin Schambach's user avatar
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

Display entries by category (multiple categories)

You can use .relatedTo(category) {% for category in craft.categories.group('categoryName') %} <h2>{{category.title}}</h2> {% for entry in craft.entries.section('news').relatedTo(...
Marco Schuler's user avatar
6 votes
Accepted

Craft 3 - Can't access the craft() in plugin controller

craft() is gone in Craft 3; instead you will be accessing Craft::$app-> for most things. I'd suggest generating your Craft 3 plugin scaffolding at pluginfactory.io, and if you leave code comments ...
andrew.welch's user avatar
  • 11.6k
5 votes
Accepted

Product Categories, Structures and Channels

Typically, I will create a single for each product "home" page, and then a channel for my products or sub-pages. I will generally use a channel vs. a structure unless I know I want to manually control ...
Damon's user avatar
  • 4,706
5 votes
Accepted

Magazine possible in Craft?

you can do all of that with Craft CMS, the thing you're searching for are "related entries" where you can relate news to a certain movie ... director ... basically you can relate everything to ...
mike-orez's user avatar
  • 111
5 votes
Accepted

How to use category in auto generated title?

In the Title Format field, try: {categoryFieldHandle[0].title}
Aaron Berkowitz's user avatar
5 votes
Accepted

Sorting Entries field results by category

It’s not totally clear from your question where you’re building the relation to the categories, but I have a feeling that this is the solution you’re looking for. {% set groupParam = '{ object....
carlcs's user avatar
  • 36.2k
5 votes
Accepted

Grouping entries by category - most efficient approach

One improvement I think could be made is to fetch only the ID's of your hotTubs entries at the start of your code. You're only using this to get the related categories and fetching ID's is faster than ...
Tom De Smet's user avatar
  • 1,456
4 votes
Accepted

Output entries sorted by category title and paginate

Not exactly what you're looking for, but it gets you closer. This should display the order you want, but it doesn't have any pagination (and honestly I'm not sure if that's possible using Craft's {% ...
Brad Bell's user avatar
  • 67.5k
4 votes

Get entries by category value from within plugin

Here's another solution which feels more correct. It uses relations and the relatedTo parameter. // First get the all CategoryModels $colorCriteria = craft()->elements->getCriteria(ElementType::...
Nobster's user avatar
  • 93
4 votes
Accepted

Including two category slugs in entries’ URLs

Set your section’s Entry URL Format to this: {mySection.first.slug}/{myCategory.first.slug}/{slug} mySection.first and myCategory.first will give you the first-selected category within those ...
Brandon Kelly's user avatar
4 votes
Accepted

Loop through entries that share current entry category and restrict to Cat Group Id

You can make use of 'not' to exclude items from your parameters. An example of how this would be implemented in the above use case would be as follows {% set categories = craft.categories.slug( 'not ...
Luke Pearce's user avatar
  • 3,863
4 votes

Entries from all categories in a category group via URL?

Create a URL structure like this: yourwebsite.com/categories/groupName Then in your template: {% set group = craft.request.getSegment(2) %} {% set categories = craft.categories.group(segment) %} ...
mcclaskiem's user avatar
4 votes
Accepted

Find matching asset (image) for category

I would add the brand category field to the assets field set. This way you can add brands to images and use .relatedTo to find them. <ul> {% for category in craft.categories.group( 'brands' ) ...
Luke Pearce's user avatar
  • 3,863
4 votes
Accepted

Show all tags related to a category?

You can use the relatedTo param on craft.tags like you can with any Element inside Craft. All Elements inside Craft can be related to one another. There is a .relatedTo parameter available on all of ...
Luke Pearce's user avatar
  • 3,863
4 votes
Accepted

Are categories the best way to organize Commerce product filters?

Hard to say if they're the 'best' tool, but I don't see anything here you can't do with Craft categories and they seem the logical and built in way to go about it. In essence you simply look at your ...
Jeremy Daalder's user avatar
4 votes
Accepted

How to separate a parent and it's children when looping through a category?

If you have a 2-level category, and you select one of the nested categories (Los Angeles), what you get in your entry is Los Angeles and its parent (California). There is no way to have Los Angeles ...
Marion Newlevant's user avatar
4 votes
Accepted

Query all categories once and get individual categories from array

Your issue is that as soon as you call .find(), your ElementCriteriaModel returns an array of elements. In other words, the ElementCriteriaModel is gone, and it's no longer possible to add parameters (...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Show Other Entries in Same Category

Based on your description, this should do it: {% if entry.style|length %} <aside> <h4 class="text-center">Related Posts</h4> <div class="columns"> <ul class=...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Categories not adding trailing slash

Whoops! Sorry. As I went to check the code I realised, I was using {{ category.uri }} instead of {{ category.url }}.
Terry Upton's user avatar
  • 1,894
4 votes
Accepted

Related Items in a Category not Appearing in Results

The code above was missing a space after not .id('not'~entry.id) should have been .id('not '~entry.id) This one is an easy one to miss and I have done it a million times.
AbbeyDesign's user avatar
  • 1,527
4 votes
Accepted

How/Can does Craft handle ancestor/grandparent relationships?

As usual with Craft you have several options. If you didn't need the index pages, I would suggest a location category, which you organized into 3 levels: states, regions, counties. Selecting a county ...
Marion Newlevant's user avatar

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