We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40131
    • 40 Posts
    I am a newbee and struggling with the following sorting scenario:

    Here is a simple structure:


    country 1

    ______area 1

    ____________hotel 1

    ____________hotel N

    ______area 2

    ____________hotel 1 - N

    and so on..

    contry N

    ______area N

    ____________hotel 1 - N



    "country" is the main parents in site tree. We have sub-parents for "area" and then children as "hotels"

    the sorting should be:

    Output all hotels in a country, while the top 10 results are the latest published date DESC, and the rest are other sorted ASC by pagetitle e.g.

    [top results - 1 - 10 desc by publish date]

    beneath them

    N results sorted by Name Asc


    Any help will be appreciated.
      • 4172
      • 5,888 Posts
      [top results - 1 - 10 desc by publish date] - stay allways there ?

      N results sorted by Name Asc - with pagination ?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 40131
        • 40 Posts
        In fact yes. We should have pagination for N results. (10 on top (latest), but only on the first page)
          • 4172
          • 5,888 Posts
          something like that snippet should do it:
          first page the 10 latest next pages the rest sorted by pagetitle:

          snippet-call:

          [[!getPage? &element=`getResourcesLatestFirst` &parent=`5` &tpl=`resourcelist` &limit=`10`]]
          <div class="pageNav">[[!+page.nav]]</div>
          


          snippet getResourcesLatestFirst:
          <?php
          
          $page = $modx->getOption('page', $_REQUEST, 1);
          $parent = $modx->getOption('parent', $scriptProperties, $modx->resource->get('id'));
          $outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, '');
          $limit = isset($limit) ? (integer)$limit : 5;
          $offset = isset($offset) ? (integer)$offset - $limit : 0;
          $totalVar = !empty($totalVar) ? $totalVar : 'total';
          
          $childs = $modx->getChildIds($parent);
          
          $where = array();
          $where['isfolder'] = '0';
          $where['id:IN'] = $childs;
          $where['deleted'] = '0';
          $where['published'] = '1';
          
          //get the latest 10
          $c = $modx->newQuery('modResource');
          $c->select($modx->getSelectColumns('modResource'));
          $c->where($where);
          $c->sortby('publishedon','DESC');
          
          $modx->setPlaceholder($totalVar, $modx->getCount('modResource', $c));
          
          $c->limit($limit);
          
          $output = array();
          
          if ($collection = $modx->getCollection('modResource', $c)) {
              foreach ($collection as $object) {
                  $latestids[] = $object->get('id');
                  if ($page < 2) {
                      $row = $object->toArray();
                      $output[] = $modx->getChunk($tpl, $row);
                  }
              }
          }
          
          if ($page > 1) {
              //get the other ones
              $where['id:IN'] = array_diff($childs, $latestids);
          
              $c = $modx->newQuery('modResource');
              $c->select($modx->getSelectColumns('modResource'));
              $c->where($where);
              $c->sortby('pagetitle');
          
              $modx->setPlaceholder('total', $modx->getCount($c));
          
              $c->limit($limit, $offset);
          
              if ($collection = $modx->getCollection('modResource', $c)) {
                  foreach ($collection as $object) {
                      $row = $object->toArray();
                      $output[] = $modx->getChunk($tpl, $row);
                  }
              }
          }
          
          return implode($outputSeperator, $output);
          
          
          
          [ed. note: Bruno17 last edited this post 14 years, 4 months ago.]
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 40131
            • 40 Posts
            Thx a lot Bruno17, you are great!
            I tried but I have no luck since my setup is a bit complicated. In fact the site tree is a bit diffent. I will try to explain (with the pic attached; sorry it is in Bulgarian but I color coded the elements)

            - orange represents "country"
            - magenta is "Area"
            - green is "hotel parent" (only as a logic folder organization)
            - red is the actual "hotel child page"

            And the results should be listed in Page id (5) - cyan color.

            My current getpage call in id (5) is:

            [[!getPage:default=`<div class="nothing_found">No results!</div>`@MyPropSet?
             &elementClass=`modSnippet`
             &element=`getResources`
             &parents=`[[+get_parent_placeholder:is=``:then=`4`:else=`[[+get_parent_placeholder]]`]]`
             &includeTVs=`1`
             &includeTVList=`HotelNearTracksTV,HotelStarsTV,HotelMapTV,HotelImageTV,ShowOnHomeTV`
             &tpl=`HotelListTpl`
             &where=`{"template:=":6}`
             [[!hotels_filter]]
             &limit=`5`
            ]]