<p>There are some errors in your code – maybe those just snuck in when you posted the code here, but right now the hook can't work correctly:</p>
<ul>
<li><code>$elementQuery</code> is undefined – did you mean <code>$event-&gt;elementQuery</code>?</li>
<li><code>$elementIdsArray</code> is also not defined.</li>
<li>Make sure you're importing the correct <code>Search</code> class (<code>craft\services\Search</code>).</li>
</ul>
<p>Besides that, I don't think the <code>SearchEvent::setElementIds</code> function is intended to set element IDs to be used for the query. If you look at the <a href="https://github.com/craftcms/cms/blob/develop/src/services/Search.php#L275-L282" rel="nofollow noreferrer">source code</a>, the <code>SearchEvent</code> created for this hook is not saved as a variable and the method doesn't check if you've set any element IDs on the event:</p>
<pre class="lang-php prettyprint-override"><code>// Fire a 'beforeSearch' event
if ($this-&gt;hasEventHandlers(self::EVENT_BEFORE_SEARCH)) {
    $this-&gt;trigger(self::EVENT_BEFORE_SEARCH, new SearchEvent([
        'elementQuery' =&gt; $elementQuery,
        'query' =&gt; $searchQuery,
        'siteId' =&gt; $siteId,
    ]));
}
</code></pre>
<p>If you set the element IDs on the element query itself, the hook starts working correctly:</p>
<pre class="lang-php prettyprint-override"><code>use craft\services\Search;
use craft\events\SearchEvent;

Event::on(
    Search::class,
    Search::EVENT_BEFORE_SEARCH,
    function (SearchEvent $event) {
        $elementQuery = $event-&gt;elementQuery;
        $elementQuery-&gt;id([123, 455]);
    }
);
</code></pre>
<p>Now the last thing to check is if your condition is failing for some reason. A simple way to test this is to put a <a href="https://docs.craftcms.com/api/v3/craft.html#method-dd" rel="nofollow noreferrer">Craft::dd</a> statement in there. Then go to Admin -&gt; Entries in the backend, open the devtools in your browser and execute a search. Look for the search request executed by JavaScript, open it and look at the <code>Response</code> tab. There you can see the dumped value.</p>

CC BY-SA 4.0
2021-10-29T07:51:22.297Z
2021-10-29T07:51:22.297Z
MoritzLost
<p>Looking at a means to perform subqueries against certain searches in the back-end of the Craft CMS eg for searches against particular sections within Admin &gt; Entries.</p>
<p>Looking into this it seems like the <code>SearchEvent</code> event might be best, and specifically <code>EVENT_BEFORE_SEARCH</code>. Was hoping can pass an array of specific elementIds into the event to tighten up the number of results returned but that doesn't seem to be working.</p>
<p>Example code:</p>
<pre><code>  Event::on(
    Search::class,
    Search::EVENT_BEFORE_SEARCH,
    function (SearchEvent $event) {
      $sender = $event-&gt;sender;
      $sectionId = $elementQuery-&gt;sectionId;
      if ($sectionId == [4]) {
        $event-&gt;setElementIds($elementIdsArray); // eg [123,455], etc
      }

  });

</code></pre>
<p>However this is ignore and the usual search takes place.
Can anyone advise the best way to limit back-end searches to certain elementIds?</p>
<p>Cheers,</p>
<p>Cole</p>

<plugin-development><search><events><api>
Manipulating search event
# Manipulating search event

(Asked by: username_0 on 2021-10-28)

Looking at a means to perform subqueries against certain searches in the back-end of the Craft CMS eg for searches against particular sections within Admin > Entries.
Looking into this it seems like the SearchEvent event might be best, and specifically EVENT_BEFORE_SEARCH. Was hoping can pass an array of specific elementIds into the event to tighten up the number of results returned but that doesn't seem to be working.
Example code:
  Event::on(
    Search::class,
    Search::EVENT_BEFORE_SEARCH,
    function (SearchEvent $event) {
      $sender = $event->sender;
      $sectionId = $elementQuery->sectionId;
      if ($sectionId == [4]) {
        $event->setElementIds($elementIdsArray); // eg [123,455], etc
      }

  });


However this is ignore and the usual search takes place.
Can anyone advise the best way to limit back-end searches to certain elementIds?
Cheers,
Cole

**Tags:** plugin-development, search, events, api


---

## 1 answers

### Answer by username_1 on 2021-10-29. Score: 1

There are some errors in your code – maybe those just snuck in when you posted the code here, but right now the hook can't work correctly:

$elementQuery is undefined – did you mean $event->elementQuery?
$elementIdsArray is also not defined.
Make sure you're importing the correct Search class (craft\services\Search).

Besides that, I don't think the SearchEvent::setElementIds function is intended to set element IDs to be used for the query. If you look at the source code, the SearchEvent created for this hook is not saved as a variable and the method doesn't check if you've set any element IDs on the event:
// Fire a 'beforeSearch' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_SEARCH)) {
    $this->trigger(self::EVENT_BEFORE_SEARCH, new SearchEvent([
        'elementQuery' => $elementQuery,
        'query' => $searchQuery,
        'siteId' => $siteId,
    ]));
}

If you set the element IDs on the element query itself, the hook starts working correctly:
use craft\services\Search;
use craft\events\SearchEvent;

Event::on(
    Search::class,
    Search::EVENT_BEFORE_SEARCH,
    function (SearchEvent $event) {
        $elementQuery = $event->elementQuery;
        $elementQuery->id([123, 455]);
    }
);

Now the last thing to check is if your condition is failing for some reason. A simple way to test this is to put a Craft::dd statement in there. Then go to Admin -> Entries in the backend, open the devtools in your browser and execute a search. Look for the search request executed by JavaScript, open it and look at the Response tab. There you can see the dumped value.


---

