Jump to content

Limiting search results


Peter Knight
 Share

Recommended Posts

I have about 50 pages which are just containers for their child pages.

What's the most efficient way to exclude these parent pages from Searches?

I'm using Ryan's code here

Quote

<?php

				// search.php template file
				// See README.txt for more information. 

				// look for a GET variable named 'q' and sanitize it
				$q = $sanitizer->text($input->get->q); 

				// did $q have anything in it?
				if($q) { 

					// Sanitize for placement within a selector string. This is important for any 
					// values that you plan to bundle in a selector string like we are doing here.
					$q = $sanitizer->selectorValue($q); 

					// Search the title and body fields for our query text.
					// Limit the results to 50 pages. 
					$selector = "title|body~=$q, limit=50"; 

					// If user has access to admin pages, lets exclude them from the search results.
					// Note that 2 is the ID of the admin page, so this excludes all results that have
					// that page as one of the parents/ancestors. This isn't necessary if the user 
					// doesn't have access to view admin pages. So it's not technically necessary to
					// have this here, but we thought it might be a good way to introduce has_parent.
					if($user->isLoggedin()) $selector .= ", has_parent!=2"; 

					// Find pages that match the selector
					$matches = $pages->find($selector); 

					// did we find any matches? ...
					if($matches->count) {

						// we found matches
						echo "<strong>$matches->count page(s) match your query:</strong>";

						// output navigation for them
						echo "<ul class='uk-list uk-list-divider'>";

						foreach($matches as $match) {
							echo "<li><a href='$match->url'>$match->title</a>";
							echo "<div class='summary'>$match->summary</div>";
							echo "<div class='search-path'>{$match->path}</div></li>";

						}
						 
						

						echo "</ul>";

					} else {
						// we didn't find any
						echo "<h2>Sorry, no results were found.</h2>";
					}

				} else {
					// no search terms provided
					echo "<h2>Please enter a search term in the search box (upper right corner)</h2>";
				}

	?>

 

 

I guess I have a few options but is number 2 here the best way or is there anything I haven't considered which would be better?

1. Mark as 'Hidden: Exclude from lists and searches"
Isn't an option because it hides from lists which I use.

2. Tell the selector to ignore a particular template(s)
Sounds like a good approach

3. Create my own checkbox called "Search Ignore" and add it into a selector
Could work but it seems like a ton of work to manually edit all the pages I want to ignore

Cheers

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

22 minutes ago, Peter Knight said:

Tell the selector to ignore a particular template(s)
Sounds like a good approach

this seems like the best approach. If these parents have a distinctive feature that can be used to select them (a certain parent, template, field etc), then use $pages->getIDs() method and update your selector to exclude the children with $pages("..., has_parent!=$hidableParents");

Edit: has_parent supports single values, you can build a string for multiple OR groups (has_parent=!35), (has_parent=!72), (...) etc

  • Like 1
Link to comment
Share on other sites

^ Templates seem the most likely option. I usually have parent pages a different template from children, so you could do "template!=example", or specify the child templates, i.e. "template=child-example".

If the child pages don't have any child pages of their own, you can also exclude them on that basis?

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...