0

I want to write a more complex autocomplete search for a craft site. I know there are already some posts here that go in that direction. As they also tried, I approach this problem first by using the Element API. However, it does not work nicely with multiple queries.

That's my situation: I have a database with items that have also a category assigned to them and sometimes are linked to a second section.

I would now like the autocomplete to show 3 types of results:

  1. Entries where the title matches the search term (this can be easily done with Element API)
  2. The categories that match the search term, labelled with "Jump to Category: Title"
  3. The entries of the linked section that match the search term, also labelled accordingly.

So the autocomplete suggestions at the end would list all three types of entries, labelled accordingly.

I can obviously run three different calls to element API but that seems rather inconvenient and ineffective.

Is there any better way to do this?

1
  • Is there a particular reason why you need to use the Element API? I would do this with an ordinary Twig template, which then gives you all the simplicity of regular craft.entries() and craft.categories() calls. You can then differentiate the types using e.g. item is instance of('craft\\elements\\Category') and/or use the group and merge filters according to your needs. Apr 29 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.