<p>I finally figured everything out, It took a long time to figure out what I actually needed to search for to find the answers I needed.</p>

<p>I put a comment at the bottom of <a href="https://craftcms.stackexchange.com/questions/1878/advanced-search">this thread</a> that was also of tremendous help. Passing multiple arguments to find related entries via categories.</p>

CC BY-SA 3.0
2015-09-16T18:26:14.697Z
2015-09-16T18:26:14.697Z
2017-04-13T12:51:00.160Z
Damon
<p>I am attempting to create a relationship between products.
I have a section named "product" which is a Structure. Each "product" entry has a category field called "species" and a category field called "category".  </p>

<p>My "species" category is a nested category. For example:</p>

<pre><code>Dogs
  |-- Dachshund
  |-- Chiwawa
Cats
  |-- Long Hair
  |-- Short Hair
</code></pre>

<p>My "category" category is a single list. For example:</p>

<pre><code>Yellow
Green
Blue
</code></pre>

<p>I have a form that I am trying to filter back my entries based on which "species" and/or "category" is chosen from the drop-down menu(s).</p>

<p>I have the ajax part down - no problem. I am sending a request to a twig <code>.json</code> template, and I am always getting into my "No Results" condition.</p>

<p>Here is the twig syntax I'm using. I have to be close (hopefully)...</p>

<pre><code>{% set species = craft.request.getParam('species') %} // returns "Dachshund"
{% set category = craft.request.getParam('category')  // returns "Yellow"

{% set products = craft.entries.section('product').relatedTo('or',
{ sourceElement: category, field: 'category' },
{ targetElement: species, field: 'species' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                    {
                        "name": "{{ product.title }}"
                    } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}
</code></pre>

<p>I have also been trying to follow <a href="https://craftcms.stackexchange.com/questions/4414/search-contact-person-plugin-via-ajax/4415#4415">this thread</a> in SO to search for entries.</p>

<p>I know I need a conditional (this or that) since a user could choose a "species" but not a "category" so <a href="http://buildwithcraft.com/docs/relations" rel="nofollow noreferrer">this article</a> from the docs is where I need to be.</p>

<p>I have gotten results back, but then run into an error that a class can't be converted to a string. I assume that's because I don't have the syntax correct to dig down into the category group correctly.</p>

<p>Within my "product" entry, the "species" category field looks like this:</p>

<pre><code>Dogs
  |--Dachshund
</code></pre>

<p>I am also 100% confident the <a href="https://github.com/pixelandtonic/ElementAPI" rel="nofollow noreferrer">ElementAPI</a> would be extremely helpful, I just haven't figured out how to incorporate it.</p>

<p><strong>EDIT</strong></p>

<p>Here is my updated <code>products.json</code> file that I am submitting the form to. I still fall into the <code>NO_RESULTS</code> condition.</p>

<pre><code>{% if craft.request.isAjax %}
{% set species = craft.request.getParam('species').first() %}
{% set category = craft.request.getParam('category').first() %}

{% set foo = craft.entries.section('product').species(species) %}
{% set bar = craft.entries.section('product').category(category) %}

{% set products = craft.entries.section('product').relatedTo('or',
    { sourceElement: foo, field: 'species' },
    { targetElement: bar, field: 'category' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                  {
                    "name": "{{ product.title }}"
                  } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}
{% endif %}
</code></pre>

<categories><relations>
Search for related entries via AJAX given multiple parameters
# Search for related entries via AJAX given multiple parameters

(Asked by: username_0 on 2015-09-15)

I am attempting to create a relationship between products.
I have a section named "product" which is a Structure. Each "product" entry has a category field called "species" and a category field called "category".  
My "species" category is a nested category. For example:
Dogs
  |-- Dachshund
  |-- Chiwawa
Cats
  |-- Long Hair
  |-- Short Hair

My "category" category is a single list. For example:
Yellow
Green
Blue

I have a form that I am trying to filter back my entries based on which "species" and/or "category" is chosen from the drop-down menu(s).
I have the ajax part down - no problem. I am sending a request to a twig .json template, and I am always getting into my "No Results" condition.
Here is the twig syntax I'm using. I have to be close (hopefully)...
{% set species = craft.request.getParam('species') %} // returns "Dachshund"
{% set category = craft.request.getParam('category')  // returns "Yellow"

{% set products = craft.entries.section('product').relatedTo('or',
{ sourceElement: category, field: 'category' },
{ targetElement: species, field: 'species' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                    {
                        "name": "{{ product.title }}"
                    } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}

I have also been trying to follow this thread in SO to search for entries.
I know I need a conditional (this or that) since a user could choose a "species" but not a "category" so this article from the docs is where I need to be.
I have gotten results back, but then run into an error that a class can't be converted to a string. I assume that's because I don't have the syntax correct to dig down into the category group correctly.
Within my "product" entry, the "species" category field looks like this:
Dogs
  |--Dachshund

I am also 100% confident the ElementAPI would be extremely helpful, I just haven't figured out how to incorporate it.
EDIT
Here is my updated products.json file that I am submitting the form to. I still fall into the NO_RESULTS condition.
{% if craft.request.isAjax %}
{% set species = craft.request.getParam('species').first() %}
{% set category = craft.request.getParam('category').first() %}

{% set foo = craft.entries.section('product').species(species) %}
{% set bar = craft.entries.section('product').category(category) %}

{% set products = craft.entries.section('product').relatedTo('or',
    { sourceElement: foo, field: 'species' },
    { targetElement: bar, field: 'category' }
) %}

{
    "results": [{
        {% if products|length %}
            "status": "OK",
            "products": [
                {% for product in products %}
                  {
                    "name": "{{ product.title }}"
                  } {% if not loop.last %},{% endif %}
                {% endfor %}
            ]
        {% else %}
            "status": "NO_RESULTS"
        {% endif %}
    }]
}
{% endif %}

**Tags:** categories, relations

---

## 1 answers

### Answer by username_0 on 2015-09-16. Score: 2 | Accepted answer

I finally figured everything out, It took a long time to figure out what I actually needed to search for to find the answers I needed.

I put a comment at the bottom of [this thread](https://craftcms.stackexchange.com/questions/1878/advanced-search) that was also of tremendous help. Passing multiple arguments to find related entries via categories.


---

