<p>I'm working on the search Autocomplete based on this article,
<a href="http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms" rel="nofollow noreferrer">http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms</a></p>

<p>Besides the title, I want to search within the category too. How can I do that? The below code doesn't work as expected.</p>

<pre><code>'/search.json' =&gt; function() {
    $criteria = [
        'section' =&gt; 'products', 
        'search' =&gt; (Craft::$app-&gt;request-&gt;getParam('query')) ? 'title:'.'*'.(Craft::$app-&gt;request-&gt;getParam('query')).'*'.' OR ' . 'categories:'.'*'.(Craft::$app-&gt;request-&gt;getParam('query')).'*' : '',
        'orderBy' =&gt; 'title asc'
    ];

    return [
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; $criteria,
        'pretty' =&gt; true,
        'paginate' =&gt; false ,                
        'resourceKey' =&gt; 'search',
        'transformer' =&gt; function(craft\elements\Entry $entry) {
            return [
                'title' =&gt; $entry-&gt;title . '-' . $entry-&gt;categories,
                'url' =&gt; $entry-&gt;url,
            ];
        },
    ];
},
</code></pre>

<p>Here is my function,</p>

<pre><code>$('#search').autocomplete({
    serviceUrl: '/search.json',
    appendTo: $('.auto-complete'),
    onSelect: function (suggestion) {
        window.location.href = suggestion.url;
    },
    transformResult: function(response){
        return {
            suggestions: $.map(JSON.parse(response).data, function(dataItem) {
                return { 
                    value: dataItem.title, 
                    url: dataItem.url 
                };
            })
        };
    }
});
</code></pre>

<p>My category is under 'craft.categories.group('prodCats')'</p>

CC BY-SA 4.0
2020-04-19T08:12:24.993Z
Hi, maybe you could split this up into 2 queries. At first you search products by title and secondly search categories by title and return all entries that are related to that categories.
Johannes
CC BY-SA 4.0
2020-04-20T09:31:58.497Z
@Johannes You mean split it during the autocomplete function? Or during the element-api?
Lee
<search><plugin-elementapi><category><params><jquery>
Search for category and title in Element API for Jquery Autocomplete
# Search for category and title in Element API for Jquery Autocomplete

(Asked by: username_0 on 2020-04-15)

I'm working on the search Autocomplete based on this article,
http://craftsnippets.com/articles/search-autocomplete-component-for-craft-cms
Besides the title, I want to search within the category too. How can I do that? The below code doesn't work as expected.
'/search.json' => function() {
    $criteria = [
        'section' => 'products', 
        'search' => (Craft::$app->request->getParam('query')) ? 'title:'.'*'.(Craft::$app->request->getParam('query')).'*'.' OR ' . 'categories:'.'*'.(Craft::$app->request->getParam('query')).'*' : '',
        'orderBy' => 'title asc'
    ];

    return [
        'elementType' => Entry::class,
        'criteria' => $criteria,
        'pretty' => true,
        'paginate' => false ,                
        'resourceKey' => 'search',
        'transformer' => function(craft\elements\Entry $entry) {
            return [
                'title' => $entry->title . '-' . $entry->categories,
                'url' => $entry->url,
            ];
        },
    ];
},

Here is my function,
$('#search').autocomplete({
    serviceUrl: '/search.json',
    appendTo: $('.auto-complete'),
    onSelect: function (suggestion) {
        window.location.href = suggestion.url;
    },
    transformResult: function(response){
        return {
            suggestions: $.map(JSON.parse(response).data, function(dataItem) {
                return { 
                    value: dataItem.title, 
                    url: dataItem.url 
                };
            })
        };
    }
});

My category is under 'craft.categories.group('prodCats')'

**Tags:** search, plugin-elementapi, category, params, jquery

> **Comments:**
> **username_1**: Hi, maybe you could split this up into 2 queries. At first you search products by title and secondly search categories by title and return all entries that are related to that categories.  
> **username_0**: @username_1 You mean split it during the autocomplete function? Or during the element-api?  

---

## 0 answers

