We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38517
    • 4 Posts
    In a getResources template i have the field
    [[+createdby:userinfo=`state`]]

    is there a way of filtering results by user fields?
    where createdby:userinfo=`state` == 'devon' for example.

    or has some had experience trying anything similar.
      • 3548
      • 102 Posts
      Not with getResources.

      I would encourage you to write your own snippet for filtering resources by user data.
        Antonio Zdilar
        linearvector.com
        • 38517
        • 4 Posts
        Thanks i thought this would be the case, but that maybe as part of the blog development then displaying by author maybe possible.

        i have started to write a snippet but my knowledge of php and xPDO is basic, any suggestions welcome
        <?php
        /* first, build the query */
        $criteria = $modx->newQuery('modUserProfile');
        /* &state= provided in the snippet call */
        $criteria->where(array(
          'state' => $state,
        ));
        /* get the users as xPDOObjects */
        $users = $modx->getCollection('modUserProfile',$criteria);
        
        
        /* next query */
        $c = $modx->newQuery('modResource');
        /*  only published resources add more params later */
        $c->where(array(
          'published' => true,
        ));
        
        $c->sortby('createdon','DESC');
        /* get the resources as xPDOObjects */
        $resources = $modx->getCollection('modResource',$c);
        
        $output = ' ';
        
        foreach ($users as $user) {
            $id = $user->get('id');
            foreach ($resources as $resource) {
                $creator = $resource->get('createdby');
                if ($creator == $id) {
                    $output .= $modx->getChunk('testTPL',array(
                        'pagetitle' => $resource->get('pagetitle'),
                        'id' => $resource->get('id'),
                    ));
                }
            }
        }
        return $output;

          • 3548
          • 102 Posts
          I guess your could would work, but it is not optimal at all.

          This should work:

          $c = $modx->newQuery('modResource');
          $c->innerJoin('modUser', 'CreatedBy');
          $c->innerJoin('modUserProfile', 'Profile', 'CreatedBy.id=Profile.internalKey');
          $c->where(array(
            'published' => TRUE,
            'Profile.state' => 'Croatia',
          )); 
          $resources = $modx->getCollection('modResource',$c);
          foreach($resources as $resource){ 
            echo $resource->id;
            //do what ever you want
          }
          
            Antonio Zdilar
            linearvector.com
            • 4172
            • 5,888 Posts
            you can try something like that:

            <?php
            
            /* first, build the query */
            $criteria = $modx->newQuery('modUserProfile');
            /* &state= provided in the snippet call */
            $criteria->where(array('state' => $state, ));
            /* get the users as xPDOObjects */
            $users = $modx->getCollection('modUserProfile', $criteria);
            
            foreach ($users as $user) {
                $ids[] = $user->get('internalKey');
            }
            
            $where['createdby:IN']=$ids;
            
            return $modx->toJson($where);


            with this getResources-snippet-call:

            [[!getResources?
            ...
            ...
            &where=`[[!yoursnippet? &state=`devon`]]`
            ]]
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 38517
              • 4 Posts
              thanks for your help, i have gone with Bruno17's suggestion, i had started to go down this route using getResources but not as elegantly.

              In have added the &where to the snippet output and the &state= string is handled by FormItRetriever

              [[!getResources:ifempty=`No Results`?
              ...
              ...
              [[!mysnippet? &state=`[[+fi.state]]`]]
              ]]


              mysnippet content (aka Bruno17s) with the output rapped in if(!empty)

              <?php
              /* first, build the query */
              $criteria = $modx->newQuery('modUserProfile');
              /* &state= provided in the snippet call */
              $criteria->where(array('state' => $state, ));
              /* get the users as xPDOObjects */
              $users = $modx->getCollection('modUserProfile', $criteria);
               
              foreach ($users as $user) {
                  $ids[] = $user->get('internalKey');
              }
               
              $where['createdby:IN']=$ids;
              
              $output = '';
              
              if (!empty($state)) {
                  $output .= '&where=`';
                  $output .= $modx->toJson($where);
                  $output .= '`';
              }
              return $output;