<p>I think (well, I know) I am confused by where "regular" php is and where the ElementAPI takes over. This is returning me data, but I'm not sure if it's just dumb luck or I've actually got something working...</p>

<pre><code>$slug = craft()-&gt;request-&gt;getRequiredPost('location');  // chicago

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;group = 'Locations';
$criteria-&gt;slug = $slug;

$locations = $category = $criteria-&gt;find();

return [
    'endpoints' =&gt; [
        'myfile.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; [
                'section' =&gt; 'mysection',
                'relatedTo' =&gt; ['targetElement' =&gt; $locations],
            ],
            'transformer' =&gt; function (EntryModel $entry) {
                return [
                    'title' =&gt; $entry-&gt;title
                ];
            },
        ]
    ]
];
</code></pre>

CC BY-SA 3.0
2015-12-09T05:05:34.023Z
2015-12-09T05:05:34.023Z
Damon
<p>I am using the ElementAPI to generate a json response of data based on the value (locations) that was chosen from a drop-down.</p>

<p>I think I have all of the pieces I need, i'm struggling with how to put them all together I think.</p>

<p><strong>twig</strong></p>

<pre><code>{% set locations = craft.categories.group('locations') %}
...
{% for location in locations %}
    &lt;option value="{{ location.slug }}"&gt;{{ location.title }}&lt;/option&gt;
{% endfor %}
</code></pre>

<p><strong>elementapi.php</strong></p>

<pre><code>$slug = craft()-&gt;request-&gt;getRequiredPost('location');  // chicago
$category = craft()-&gt;categories-&gt;getGroupByHandle('Locations'); // category group
$location = ??

return [
    'endpoints' =&gt; [
        'myfile.json' =&gt; [
            'elementType' =&gt; 'Entry',
            'criteria' =&gt; [
                'section' =&gt; 'mysection',
                'relatedTo' =&gt; ['targetElement' =&gt; $location],
            ],
            'transformer' =&gt; function (EntryModel $entry) {
                return [
                    'title' =&gt; $entry-&gt;title
                ];
            },
        ]
    ]
];
</code></pre>

<categories><plugin-elementapi>
How can I get to a category via the category slug using the ElementAPI
# How can I get to a category via the category slug using the ElementAPI

(Asked by: username_0 on 2015-11-20)

I am using the ElementAPI to generate a json response of data based on the value (locations) that was chosen from a drop-down.
I think I have all of the pieces I need, i'm struggling with how to put them all together I think.
twig
{% set locations = craft.categories.group('locations') %}
...
{% for location in locations %}
    {{ location.title }}
{% endfor %}

elementapi.php
$slug = craft()->request->getRequiredPost('location');  // chicago
$category = craft()->categories->getGroupByHandle('Locations'); // category group
$location = ??

return [
    'endpoints' => [
        'myfile.json' => [
            'elementType' => 'Entry',
            'criteria' => [
                'section' => 'mysection',
                'relatedTo' => ['targetElement' => $location],
            ],
            'transformer' => function (EntryModel $entry) {
                return [
                    'title' => $entry->title
                ];
            },
        ]
    ]
];

**Tags:** categories, plugin-elementapi

---

## 1 answers

### Answer by username_0 on 2015-12-09. Score: 4 | Accepted answer

I think (well, I know) I am confused by where "regular" php is and where the ElementAPI takes over. This is returning me data, but I'm not sure if it's just dumb luck or I've actually got something working...
$slug = craft()->request->getRequiredPost('location');  // chicago

$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->group = 'Locations';
$criteria->slug = $slug;

$locations = $category = $criteria->find();

return [
    'endpoints' => [
        'myfile.json' => [
            'elementType' => 'Entry',
            'criteria' => [
                'section' => 'mysection',
                'relatedTo' => ['targetElement' => $locations],
            ],
            'transformer' => function (EntryModel $entry) {
                return [
                    'title' => $entry->title
                ];
            },
        ]
    ]
];


---

