I created Search that uses entry slug, but it fails if the slug has more than one dash "-" in it. Has anyone come across this and/or have any ideas for getting around it?

I need to use the slug because they are unique within a section.

  • 1
    Can you add a sample of the code you're using?
    – Alex Roper
    Sep 14 '18 at 23:39
  • It is a simple search: I set a variable called "q" and use that as the search parameter, e.g. craft,entries,section('rides').search(q) If that variable contains more than one dash it does not return anything - if I edit the slug to one dash it works fine
    – Roi Agneta
    Sep 14 '18 at 23:44

If you know your q variable should be a slug, you can use the .slug query parameter:

{# Get the requested entry slug from the URL #}
{% set requestedSlug = craft.app.request.getSegment(3) %}

{# Fetch the entry with that slug #}
{% set entry = craft.entries()
    .one() %}

More on how to use this in the Craft 3 docs.

I have personally had some unexpected results using Craft's built-in search.

  • Thanks, Alex . I ended up taking a different approach using relatedTo. Appreciate your response!
    – Roi Agneta
    Sep 22 '18 at 22:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.