<p>If you know your <strong>q</strong> variable should be a slug, you can use the <code>.slug</code> query parameter:</p>

<pre><code>{# 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()
    .slug(requestedSlug|literal)
    .one() %}
</code></pre>

<p><a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#slug" rel="nofollow noreferrer">More on how to use this in the Craft 3 docs</a>.</p>

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

CC BY-SA 4.0
2018-09-22T22:41:59.620Z
Thanks, Alex . I ended up taking a different approach using relatedTo. Appreciate your response!
Roi Agneta
CC BY-SA 4.0
2018-09-16T20:33:38.027Z
2018-09-16T20:33:38.027Z
Alex Roper
<p>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?</p>

<p>I need to use the slug because they are unique within a section. </p>

CC BY-SA 4.0
2018-09-14T23:39:48.030Z
Can you add a sample of the code you're using?
Alex Roper
CC BY-SA 4.0
2018-09-14T23:44:11.443Z
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
<search><entry-slug>
Search using Slug as query parameter
# Search using Slug as query parameter

(Asked by: username_0 on 2018-09-14)

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.

**Tags:** search, entry-slug

> **Comments:**
> **username_1**: Can you add a sample of the code you're using?  
> **username_0**: 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  

---

## 1 answers

### Answer by username_1 on 2018-09-16. Score: 1

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()
    .slug(requestedSlug|literal)
    .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.

> **Comments:**
> **username_0**: Thanks, Alex . I ended up taking a different approach using relatedTo. Appreciate your response!  

---

