Answers.Body: <p>An entry is not related to itself, but that's what you added as a filter.
  Use the <code>slug</code> parameter instead.</p>
  
  <pre><code>{% set categorySlug = craft.request.getSegment(2) %}
  {% set productSlug = craft.request.getSegment(3) %}
  
  {% set entries = craft.entries.section('products') %}
  
  {% if categorySlug|length %}
      {% set category = craft.categories.group('toys').slug(category).first() %}
      {% if category %}
          {% set entries = entries.relatedTo({ targetElement: category }) %}
      {% endif %}
  {% endif %}
  
  {% if productSlug|length %}
      {% set entries = entries.slug(productSlug) %}
  {% endif %}
  
  {% set product = entries.first() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-30T08:55:23.617Z
Answers.LastActivityDate: 2015-10-30T08:55:23.617Z
Answers.UserDisplayName: carlcs
Body: <p>I am using (and have used) <a href="https://craftcms.stackexchange.com/questions/6837/building-a-simple-search-by-multiple-categories">this S.O</a> post to create a product filter/search using multiple categories. Everything is working perfectly.</p>
  
  <p>I am attempting to use the same type of logic to grab ultimately a single entry.</p>
  
  <p><strong>TL;DR:</strong> I am trying to get a single entry via 2 url segments.</p>
  
  <p>I believe I am close, but what I am generating for a query isn't quite spot on as I'm always getting into my "No Products" clause.</p>
  
  <pre><code>www.mysite.com/products/toys/my-awesome-toy
  
  {% set category = craft.request.getSegment(2) %}  {# toys #}
  {% set product = craft.request.getSegment(3) %}   {# my-awesome-toy #}
  
  {% set relationParam = ['and'] %}
  
  {% if category|length %}
      {% set cat = craft.categories.group('toys').slug(category).first() %}
      {% set relationParam = relationParam|merge([{ targetElement: cat }]) %}
  {% endif %}
  </code></pre>
  
  <p><code>cat</code> is outputting <code>Toys</code> and my page renders all entries that belong to the <code>Toys</code> category group. <strong>Perfect</strong>.</p>
  
  <pre><code>{% if product|length %}
      {% set prod = craft.entries.slug(craft.request.getSegment(3)).first() %}
      {% set relationParam = relationParam|merge([{ targetElement: prod }]) %}
  {% endif %}
  
  {% set entry = craft.entries.relatedTo(relationParam).first() %}
  
  {% if entry %}
      {{ entry.title }}&lt;br&gt;
  {% else %}
      &lt;p&gt;No products found.&lt;/p&gt;
  {% endif %}
  </code></pre>
  
  <p><code>prod</code> is outputting <code>My Awesome Toy</code> and now I am getting my "No products found" message. I think it's because <code>prod</code> is outputting <code>My Awesome Toy</code> vs. <code>my-awesome-toy</code>?</p>
  
  <p>I realize I can get any entry via it's slug alone. I am using a dynamic route to create this page that looks like this:</p>
  
  <pre><code>products/tag/slug
  </code></pre>
  
  <p>I would like to get the entry strictly based on the url segments that way I can make sure my urls are valid. Meaning, I can only find "My Awesome Toy" from the "toys" category. For example:</p>
  
  <pre><code>www.mysite.com/products/toys/my-awesome-toy   {# valid #}
  www.mysite.com/products/food/my-awesome-toy   {# invalid #}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-28T22:46:59.847Z
Comments.Text: Rule out one thing. See if `my-awesome-toy` has an extra space behind it. `{% set prod = craft.entries.slug(craft.request.getSegment(3)|trim).first() %}`
Comments.UserDisplayName: a-am
Tags: <relations><url><routes>
Title: How can I get an entry based on category and slug (via url segments)?
ThreadText: # How can I get an entry based on category and slug (via url segments)?
  
  (Asked by: username_0 on 2015-10-28)
  
  I am using (and have used) [this S.O](https://craftcms.stackexchange.com/questions/6837/building-a-simple-search-by-multiple-categories) post to create a product filter/search using multiple categories. Everything is working perfectly.
  
  I am attempting to use the same type of logic to grab ultimately a single entry.
  
  **TL;DR:** I am trying to get a single entry via 2 url segments.
  
  I believe I am close, but what I am generating for a query isn't quite spot on as I'm always getting into my "No Products" clause.
  
  ```
  www.mysite.com/products/toys/my-awesome-toy
  
  {% set category = craft.request.getSegment(2) %}  {# toys #}
  {% set product = craft.request.getSegment(3) %}   {# my-awesome-toy #}
  
  {% set relationParam = ['and'] %}
  
  {% if category|length %}
      {% set cat = craft.categories.group('toys').slug(category).first() %}
      {% set relationParam = relationParam|merge([{ targetElement: cat }]) %}
  {% endif %}
  
  ```
  
  `cat` is outputting `Toys` and my page renders all entries that belong to the `Toys` category group. **Perfect**.
  
  ```
  {% if product|length %}
      {% set prod = craft.entries.slug(craft.request.getSegment(3)).first() %}
      {% set relationParam = relationParam|merge([{ targetElement: prod }]) %}
  {% endif %}
  
  {% set entry = craft.entries.relatedTo(relationParam).first() %}
  
  {% if entry %}
      {{ entry.title }}<br>
  {% else %}
      <p>No products found.</p>
  {% endif %}
  
  ```
  
  `prod` is outputting `My Awesome Toy` and now I am getting my "No products found" message. I think it's because `prod` is outputting `My Awesome Toy` vs. `my-awesome-toy`?
  
  I realize I can get any entry via it's slug alone. I am using a dynamic route to create this page that looks like this:
  
  ```
  products/tag/slug
  
  ```
  
  I would like to get the entry strictly based on the url segments that way I can make sure my urls are valid. Meaning, I can only find "My Awesome Toy" from the "toys" category. For example:
  
  ```
  www.mysite.com/products/toys/my-awesome-toy   {# valid #}
  www.mysite.com/products/food/my-awesome-toy   {# invalid #}
  
  ```
  
  **Tags:** relations, url, routes
  
  > **Comments:**
  > **username_1**: Rule out one thing. See if `my-awesome-toy` has an extra space behind it. `{% set prod = craft.entries.slug(craft.request.getSegment(3)|trim).first() %}`  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-10-30. Score: 1
  
  An entry is not related to itself, but that's what you added as a filter.
  Use the `slug` parameter instead.
  
  ```
  {% set categorySlug = craft.request.getSegment(2) %}
  {% set productSlug = craft.request.getSegment(3) %}
  
  {% set entries = craft.entries.section('products') %}
  
  {% if categorySlug|length %}
      {% set category = craft.categories.group('toys').slug(category).first() %}
      {% if category %}
          {% set entries = entries.relatedTo({ targetElement: category }) %}
      {% endif %}
  {% endif %}
  
  {% if productSlug|length %}
      {% set entries = entries.slug(productSlug) %}
  {% endif %}
  
  {% set product = entries.first() %}
  
  ```
  
  
  ---
  
  