Jump to content

Issue using selector operator %= with '->not()'


a-ok
 Share

Recommended Posts

I'm using the selector operator %= when returning search results:

$searchQuery = $sanitizer->text($input->get->q);
$searchQuery = $sanitizer->selectorValue($searchQuery);
$input->whitelist('q', $searchQuery);

$fieldsToSearch = 'title|text|textA|textarea|projectsCategories.title|projectsClients.title|aboutPressCategories.title|aboutPressClients.title';
$templatesToSearch = 'projectsSingle|shopSingle|typeSingle|typeCustomSingle|typeCustomSingleB|aboutPressSingle|repeater_modulesA';

$results = $pages->find("$fieldsToSearch%=$searchQuery, template=$templatesToSearch, sort=sort, check_access=0");

However, I'm getting an error:

ProcessWire\WireException Unknown Selector operator:  -- was your selector value properly escaped? field=, value=, selector:

If I change the operator from %= to = then no error is returned but obviously this isn't the ideal operator for what I'm doing.

However, to create even more mystery, this error seems only to be returned when using certain words. For example 'house' will return the error but 'houses' won't.

So now I'm thinking the error is actually not related to the above but actually to what follows...

$resultsToRemove = new PageArray();
foreach ($results as $result) {
	if ($result->template->name == 'repeater_modulesA' && ($result->getForPage()->is('unpublished') || $result->getForPage()->is('trash'))) {
		$resultsToRemove->add($result);
	}
}

if (count($resultsToRemove)) $results = $results->not($resultsToRemove);

$resultCount = count($results);

This part is required as I am searching repeater templates using check_access=0 but obviously don't want unpublished repeater rows or trashed repeaters to be returned so I need to do a little check. I think this is maybe where it's breaking but can't know for sure. I'm also unsure why it would break for the search query 'house' but not 'houses'?

If I can provide any more info to help me resolve this I can do... just a bit stumped.

 

Screenshot 2020-12-08 at 09.32.46.jpg

Link to comment
Share on other sites

I believe the error was in fact the use of `->not()` as I was supply a PageArray to remove and not a selector?

I simply changed this to the below which seems to have the same effect without the error?

foreach ($results as $result) {
	if ($result->template->name == 'repeater_modulesA' && ($result->getForPage()->is('unpublished') || $result->getForPage()->is('trash'))) {
		$results->remove($result);
	}
}

 

Link to comment
Share on other sites

11 hours ago, a-ok said:

if (count($resultsToRemove)) $results = $results->not($resultsToRemove);

WireArray::not() takes a selector argument, not a PageArray.

So you could do...

if (count($resultsToRemove)) $results = $results->not("id=$resultsToRemove");

...but wouldn't it be better not to have to remove any results? It's not clear to me why you are doing the unusual thing of searching repeater pages directly instead of the usual thing of searching pages that contain the repeater, e.g.

repeater_field_name.subfield_name%=$searchQuery

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, Robin S said:

It's not clear to me why you are doing the unusual thing of searching repeater pages directly instead of the usual thing of searching pages that contain the repeater

I am not sure either.

This helped me loads! Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...