1

I'm looking for a way to query all categories that have at least one relation. I'm not looking for the relatedTo() parameter, which would give me categories related to something specific.

Basically, if we talk in Wordpress terms, I'm looking for hideEmpty = true.

Is there anything like that in Craft?

Something like: craft.categories.group('products').hasRelated()
Eric Chantigny
  • 472
  • 2
  • 10
  • To get this going, I used the cached loop through all the categories and using relatedTo(category) to create a cached list of IDs. Based on this thread: https://craftcms.stackexchange.com/questions/11151/only-fetch-categories-that-are-related-to-a-minimum-of-entries?rq=1 – Eric Chantigny Oct 23 '18 at 19:13

1 Answers1

1

Add this to your chain...

.yourCategoryField(':notempty:')

It's not very well documented, but you can see it described in this old SE thread. In the docs, it's mentioned very briefly here.

Lindsey D
  • 23,974
  • 5
  • 53
  • 110
  • Hi Lindsey, Thanks for your answer. Unfortunately, that would give me all entries that have a Category associated to it. I want it the other way around. I need all Categories that were assigned at least once somewhere. – Eric Chantigny Oct 23 '18 at 17:26