<p>The results query could not be the same, you have to separate them.</p>
<pre><code>use craft\commerce\elements\Product;
use craft\elements\Entry;

return [
    'endpoints' =&gt; [
        'search/product.json' =&gt; function () {
            // settings
            $phrase   = Craft::$app-&gt;request-&gt;getParam('query');
            $criteria = [
                'search' =&gt; 'title:' . $phrase,
            ];

            return [
                'elementType' =&gt; Product::class,
                'criteria'    =&gt; $criteria,
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Product $product) {
                    return [
                        'title' =&gt; $product-&gt;title,
                        'url'   =&gt; $product-&gt;url,
                    ];
                },
            ];
        },
        'search/news.json' =&gt; function () {
            // settings
            $phrase   = Craft::$app-&gt;request-&gt;getParam('query');
            $criteria = [
                'search' =&gt; 'title:' . $phrase,
            ];

            return [
                'elementType' =&gt; Entry::class,
                'criteria'    =&gt; $criteria,
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Entry $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'url'   =&gt; $entry-&gt;url,
                    ];
                },
            ];
        },
    ]
];
</code></pre>
<p><strong>UPDATED: 18/12/2020</strong></p>
<p>Anyway, I've found a new way that allows you o use the multiple entries in an endpoint.</p>
<pre><code>&lt;?php

use craft\commerce\elements\Product;
use craft\elements\Entry;

return [
    'endpoints' =&gt; [
        'search.json' =&gt; function () {
            // settings
            $phrase         = \Craft::$app-&gt;request-&gt;getParam('query');
            $articles       = Entry::find()-&gt;search($phrase)-&gt;limit(10)-&gt;all();
            $articleObjects = [];

            if ($articles) {
                $articleTemplateName = ''; // your article template path
                foreach ($articles as $article) {
                    $articleObjects[] = \Craft::$app-&gt;getView()-&gt;renderTemplate($articleTemplateName, ['post' =&gt; $article]);
                }
            }

            return [
                'elementType' =&gt; Product::class,
                'criteria'    =&gt; [
                    'search' =&gt; 'title:' . $phrase,
                ],
                'paginate'    =&gt; false,
                'transformer' =&gt; function (Product $product) {
                    return [
                        'title' =&gt; $product-&gt;title,
                        'url'   =&gt; $product-&gt;url,
                    ];
                },
                'meta'        =&gt; [
                    'news' =&gt; $articleObjects
                ]
            ];
        },
    ]
];
</code></pre>

CC BY-SA 4.0
2020-07-30T12:12:49.933Z
2020-12-18T07:37:30.877Z
2020-12-18T07:37:30.877Z
Mostafa Soufi
<p>I am currently trying to create an autocomplete search with the element-api. I need results from two different elementTypes:</p>

<ul>
<li>Product::class</li>
<li>Entry::class</li>
</ul>

<p>How do I get results out of both elementTypes with only one endpoint?</p>

<p>Thats my working Product::class element-api.php config file:</p>

<pre class="lang-php prettyprint-override"><code>return [
    'endpoints' =&gt; [
        'search.json' =&gt; function() {
        // settings
        $phrase = Craft::$app-&gt;request-&gt;getParam('query');

        $criteria = [
            'search' =&gt; 'title:'.$phrase,
        ];

            return [
                'elementType' =&gt; Product::class,
                'criteria' =&gt; $criteria,
                'paginate' =&gt; false,
                'transformer' =&gt; function(Product $product) {
                return [
                    'title' =&gt; $product-&gt;title,
                    'url' =&gt; $product-&gt;url,
                ];
            },
          ];

        },
    ]
];
</code></pre>

<craft3><plugin-elementapi><elementtype>
Using multiple elementTypes in Element API
# Using multiple elementTypes in Element API

(Asked by: username_0 on 2019-11-24)

I am currently trying to create an autocomplete search with the element-api. I need results from two different elementTypes:

Product::class
Entry::class

How do I get results out of both elementTypes with only one endpoint?
Thats my working Product::class element-api.php config file:
return [
    'endpoints' => [
        'search.json' => function() {
        // settings
        $phrase = Craft::$app->request->getParam('query');

        $criteria = [
            'search' => 'title:'.$phrase,
        ];

            return [
                'elementType' => Product::class,
                'criteria' => $criteria,
                'paginate' => false,
                'transformer' => function(Product $product) {
                return [
                    'title' => $product->title,
                    'url' => $product->url,
                ];
            },
          ];

        },
    ]
];

**Tags:** craft3, plugin-elementapi, elementtype


---

## 1 answers

### Answer by username_1 on 2020-07-30. Score: 2

The results query could not be the same, you have to separate them.
use craft\commerce\elements\Product;
use craft\elements\Entry;

return [
    'endpoints' => [
        'search/product.json' => function () {
            // settings
            $phrase   = Craft::$app->request->getParam('query');
            $criteria = [
                'search' => 'title:' . $phrase,
            ];

            return [
                'elementType' => Product::class,
                'criteria'    => $criteria,
                'paginate'    => false,
                'transformer' => function (Product $product) {
                    return [
                        'title' => $product->title,
                        'url'   => $product->url,
                    ];
                },
            ];
        },
        'search/news.json' => function () {
            // settings
            $phrase   = Craft::$app->request->getParam('query');
            $criteria = [
                'search' => 'title:' . $phrase,
            ];

            return [
                'elementType' => Entry::class,
                'criteria'    => $criteria,
                'paginate'    => false,
                'transformer' => function (Entry $entry) {
                    return [
                        'title' => $entry->title,
                        'url'   => $entry->url,
                    ];
                },
            ];
        },
    ]
];

UPDATED: 18/12/2020
Anyway, I've found a new way that allows you o use the multiple entries in an endpoint.
 [
        'search.json' => function () {
            // settings
            $phrase         = \Craft::$app->request->getParam('query');
            $articles       = Entry::find()->search($phrase)->limit(10)->all();
            $articleObjects = [];

            if ($articles) {
                $articleTemplateName = ''; // your article template path
                foreach ($articles as $article) {
                    $articleObjects[] = \Craft::$app->getView()->renderTemplate($articleTemplateName, ['post' => $article]);
                }
            }

            return [
                'elementType' => Product::class,
                'criteria'    => [
                    'search' => 'title:' . $phrase,
                ],
                'paginate'    => false,
                'transformer' => function (Product $product) {
                    return [
                        'title' => $product->title,
                        'url'   => $product->url,
                    ];
                },
                'meta'        => [
                    'news' => $articleObjects
                ]
            ];
        },
    ]
];


---

