Jump to content

API Problems with implementing search


JBrinker
 Share

Recommended Posts

Hi there,

so, I'm quite new to ProcessWire, I am currently building my first site with it, I'm almost done, everything went smoothly.. up until now.

I've been trying to build a search function for hours now and I did get it to work after a while, but not in a very satisfactory manner. As a starting point I used the code from the "site-classic" templates, but the problem is: API-calls don't seem to work.

It all starts off with

if ($q = $sanitizer->selectorValue($input->get->q)) {

Which gives me the following "Error: Call to a member function selectorValue() on a non-object"

Investigating lead me to finding, that it won't just call selectorValue, but also, that $input->get->q does not exist. Actually $input itself is null and so is $sanitizer. Which also leads me to not being able to whitelist q, as I can't call $input->whitelist()

The only way for me to retrieve the query value is via $_GET["q"], which does work.

So this was my main issue.

Another question would be: in the search template of site-classic it is said:

// Search the title, body and sidebar fields for our query text.
// Limit the results to 50 pages. 
// Exclude results that use the 'admin' template. 
$matches = $pages->find("title|body|sidebar~=$q, limit=50"); 

But I don't see any constraint actually excluding admin templates. If I enter the term "template" into my search, I get a nice link in my search results into my backend "/processwire/access/permissions/page-template/". How to prevent this?

I hope you can help me with my issues.

Thanks a lot!

a

Link to comment
Share on other sites

Hi there

Are you implementing this search inside a funciton or in the admin?

If either of those is the case, try wire('input') and wire('sanitizer'), wire('pages') etc. If you're writing the code inside a module you can also use $this->input and $this->sanitizer

If neither of those is the case, could you post the full code that you've got for the search page please?

  • Like 2
Link to comment
Share on other sites

And to my second question, do you have a suggestion how to solve that as well?

Just list your templates to include, or exclude the "admin" template:

$matches = $pages->find("template=template1|template2|template3, title|body|sidebar~=$q, limit=50");

or

$matches = $pages->find("template!=admin, title|body|sidebar~=$q, limit=50");
Link to comment
Share on other sites

The better way of excluding the admin is this:

$matches = $pages->find("has_parent!=2");

This excludes every child of "Admin", which has a hardcoded id of 2. That's more flexible than just excluding admin as for example users ("template=user") are also part of the admin.

  • Like 3
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

×
×
  • Create New...