Jump to content

Limiting search results

Peter Knight

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



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











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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By John W.
      Hi, have a site where I set up a news category, then under that I have different regions where editors in those regions can add news stories.  I'm trying to figure out how to allow each editor to only add/edit stories in their region, and not allow editors from other regions to edit them.
      News  (template:news-category)
           Washington County (template:news-region)
                     My First News Story for Washington County by Editor 1 (template:news-article)
           Jefferson County (template:news-region)
                     A News Story by the jefferson county editor (template:news-article)
            Taylor County (template:news-region)
                    A news story by a Taylor county editor (template:news-article)
      I really don't want to create 3 different news-region templates along with 3 different news-article variations, and 3 different types of Editor roles.
      Is there a way / module that will let me say, "jane which is an editor can only add and edit to Washington County" and "bill which is an editor can only add or edit to Taylor County", etc?  Also, it would be nice to allow the "home office" editor to add/delete/modify contents created by the other editors, a "Super Editor" if you will.
      Thanks mucho!
    • By donatas
      how would I do a multi-language website search with just a selector?
      I have many multi-lang fields and I want to do a search through all of them at once and through all of their language values.
      Is there a "selector way" of doing this? Maybe something like `title|title:de|title:it`? It seems I have seen this somewhere a long time ago but can't find in any documentation or forum search...
      Or the only way of doing it is by running separate searches for each language with output formatting off and then consolidating it all in one single results array?
      Because I still want to give users a result, even if it is in another language than current $user. Visitors mostly will be searching for specific terms that are very similar in all languages, but might be not used in one language version of a single page, for example. Or the user might not have switched language tohis prefered and did the search first, etc.. (many use cases in my situation)
      $pages->find('title~='.$q) - maybe different operator is needed? /en/search/?q=visit = 1 results /it/search/?q=visit = 0 results Thanks for any advice!
    • By sebr
      In my search page, I used a selector like this :
      $searchQuery = $sanitizer->entities($input->get('q')); $searchQuery = $sanitizer->selectorValue($searchQuery); $selector = 'title|subtitle|summary|html_body_noimg~=' . $searchQuery; $matches = $pages->find($selector); I don't have the same results if $searchQuery contains accent or not.
      For example,
      with « bâtiment » I have no result with « batiment » I have onea result : « Les bâtiments et les smart-city » Normally I should have the same results? How can I do that ?
      Thanks for your help
    • By michelangelo
      Hello guys, I am building a sort of an archive. Relatively simple, although I have about 8000 records, each with 15 fields (text, int, images, url). I created a crude search system with a form (emulating the famous Skyscrapper example) to filter through the system. Everything works but it is quite slow... I have 2 questions which are related:

      1. How can I search through the database?
      2. What is a good practice to display many records like these?
      1. I am retrieving the results with
      $songs = $pages->findMany('template=nk-song'); Then I do a foreach to render them all. I am unsure if that is a good way. If I render all of them on the page, it creates thousands of divs with a bit of text, and this can take a while (10s-15s).
      2. This one is even worse :D as every time I retrieve my desired records with something like this:
      $page->find("field_to_search_through~=my_query_string") I get between 20 and 200, but when I render them I am creating iframes with YouTube videos and that can take up to 10s to finish. I "solved" it by only loading the iframes if they are in view with IntersectionObserver on the client-side. But I feel there is a more precise PHP / ProcessWire approach.
      Just to clarify, I started doing all of this custom rendering and querying because tools like ElasticSearch or SearchEngine were a bit complicated and I needed a simple to retrieve information and then display it in my own way.
      Thank you!
    • By snobjorn
      I have a website with multiple content types that I want to be accessible through search. I really like the live search on processwire.com, that sorts content types while typing. I tried to find the code to recreate this, with no luck. Does anyone know if this is jquery, specific jquery plugins, json/xml cached files, and what kind of PHP code is used? Any tip that point me in the right direction would be much apperciated.
      The search result listing seems fairly easy to create with sorting through parameters.
  • Create New...