We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17667
    • 108 Posts
    I have created a query to pull all published resources that use template 18. The code works:
    $c = $modx->newQuery('modResource');
    $c->where(
    	'published'=>1,
    	'deleted:!='=>1,
    	'template'=>18
    );
    $collection = $modx->getCollection('modResource',$c);

    Now I need a query which chooses published resources that use template 18 *or* 19. This is what I have but it doesn't work:
    $c = $modx->newQuery('modResource');
    $c->where(
    	'published'=>1,
    	'deleted:!='=>1,
    	array('template'=>18,'OR:template'=>19)
    );
    $collection = $modx->getCollection('modResource',$c);


    I've tried a few different combinations but I can't figure out the proper syntax.
      • 4172
      • 5,888 Posts
      you can try 'IN'

      $c = $modx->newQuery('modResource');
      $c->where(array(
          'published'=>1,
          'deleted:!='=>1,
          'template:IN'=>array(18,19))
      );
      $collection = $modx->getCollection('modResource',$c);
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 17667
        • 108 Posts
        Perfect! Thanks.
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: lukemcd at Mar 30, 2012, 12:33 PM
          Now I need a query which chooses published resources that use template 18 *or* 19. This is what I have but it doesn't work:
          $c = $modx->newQuery('modResource');
          $c->where(
          	'published'=>1,
          	'deleted:!='=>1,
          	array('template'=>18,'OR:template'=>19)
          );
          $collection = $modx->getCollection('modResource',$c);


          I've tried a few different combinations but I can't figure out the proper syntax.

          FYI, you were really close with this, you just need to pass an array to the where clause, and to specify the operator explicitly when you include a conjunction in an array criteria key, e.g.
          <?php
          $c = $modx->newQuery('modResource');
          $c->where(array(
              'published' => 1,
              'deleted:!=' => 1,
              array(
                  'template' => 18,
                  'OR:template:=' => 19
              )
          ));
          $collection = $modx->getCollection('modResource',$c);

            • 17667
            • 108 Posts
            That's good to know! I am starting to get a hang of the syntax.