We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    I am setting up the SimpleSearch extension on my site but want to limit the search results to a particular Template (eg: it only returns resources that use `ProductTemplate`).

    I've looked through the RTFM but am still not sure how to do this.

    Can anyone point me in the right direction?

    Thanks!
      • 4172
      • 5,888 Posts
      would be much the same like this one:
      https://forums.modx.com/thread/95109/simplesearch-restrict-the-search-to-specific-resources-groups#dis-post-515721

      untested, some like that:

      <?php
       
      $template_ids = explode(',', $modx->getOption('template_ids', $scriptProperties, 0));
      $mode = $modx->getOption('mode', $scriptProperties, 'show'); //show or hide resources with given templates
       
      $c = $modx->newQuery('modResource');
      $c->where(array('template:IN' => $template_ids));
      if ($collection = $modx->getCollection('modResource')) {
          foreach ($collection as $object) {
              $ids[] = $object->get('id');
          }
      }
       
      if ($mode == 'hide') {
          $c = $modx->newQuery('modResource');
          $c->select($modx->getSelectColumns('modResource', 'modResource', array('id')));
          $rows = array();
          $resources = array();
          if ($c->stmt->execute()) {
              if ($results = $c->stmt->fetchAll(PDO::FETCH_ASSOC)) {
                  $rows = $results;
              }
          }
          foreach ($rows as $row){
              if (isset($row['id'])){
                  $resources[] =  $row['id'];
              }
          }
          $scriptProperties['ids'] = implode(',', $resources);
          $scriptProperties['exclude'] = implode(',', $ids);
       
      } else {
          $scriptProperties['ids'] = implode(',', $ids);
      }
       
      return $modx->runSnippet('SimpleSearch', $scriptProperties);
      


      call it as you would call simpleSearch, but with additional properties, like

      [[!mySimpleSearch?
      &template_ids=`3,4,5`
      &idType=`documents`
      &mode=`hide`
      &otherproperties...
      ]]



      or if you want to show only resouces with given templates:

      [[!mySimpleSearch?
      &template_ids=`3,4,5`
      &idType=`documents`
      &otherproperties...
      ]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!