We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38142
    • 91 Posts
    Noob-level question: Trying to use getResources to build the kind of nested list of dated resources seen in Wordpress archives (producing more than the summary month list that Archivist produces), using a date TV instead of the published on setting, and have no idea how to collect the array of resources for a particular month belonging to a particular parent, effectively doing something like this (which I am certain is wrong):

    getResources? &where=`{"date:=":2017,"date:=":june}` &sortbyTV=`date` &sortdirTV=`ASC` &parents=`6`

    What is the right way to do it?

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 38142
        • 91 Posts
        Quote from: Bruno17 at Aug 31, 2017, 09:44 AM
        seems related to https://forums.modx.com/thread/102744/getresources-with-tv-date-filter-with-specific-month-and-year#dis-post-553463

        That looks massively hopeful and will give it a go. Thank you very much for pointing that out.
          • 38142
          • 91 Posts
          Just to confirm that this works:

          $tvfilter = 'date<<2017-05-31,date>>2017-05-00';
          $parents = '6';

          $output = $modx->runSnippet('getResources',array(
          'parents' => $parents,
          'tpl' => 'newslistrow',
          'limit' => '90',
          'depth' => '2',
          'showHidden' => 0,
          'includeContent' => 0,
          'includeTVs' => 1,
          'processTVs' => 1,
          'tvPrefix' => '',
          'sortbyTV' => 'date',
          'tvFilters' => $tvfilter,
          'sortdirTV' => 'ASC'
          ));

          Still a long, long way to a nested list, but one major obstacle tackled.

          Thank you again.
            • 4172
            • 5,888 Posts
            what do you mean with a nested list?

            maybe this is possible with migxLoopCollection, what you have in mind.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 38142
              • 91 Posts
              Quote from: Bruno17 at Aug 31, 2017, 11:11 AM
              what do you mean with a nested list?

              maybe this is possible with migxLoopCollection, what you have in mind.

              I just mean exactly the sort of thing that Wordpress shows in blog sidebars, where you have a list of years and months for which there are posts. In my case it is news items, and the client wants a dropdown list for each month where there is a news item.

              No clue what best practice is, but this seems to be working:

              Each news item resource has a date TV to hold the date of the news, and is also made the child of a parent resource whose name is the year for the news. The year container resources are in turn children of the main news resource (with ID 6 in this case).

              Resource for showing the list has this snippet call: [[getyears]]

              The getyears snippet aims to list the years for which there is news (years are stored as pagetitles of the 1st generation children of the main news parent):

              $tpl = 'mynewsyearrow';
              $parents = 6;
              $start = '<ul class="year">';
              $end = '</ul>';
              
              $getnewsyears = $modx->runSnippet('getResources',array(
                 'parents' => $parents,
                 'limit' => '50',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 0,
                 'tpl' => 'mynewsyearrow'
              ));
              if ($getnewsyears) {   $o = $start . $getnewsyears . $end ; return $o; } else {  echo '<p>No news items found.</p>';}


              That calls the mynewsyearrow chunk which looks like this:

              <li class="year">[[+pagetitle]]
              [[getmonths? &docid=[[+id]]]]
              </li> 


              That calls another snippet to list the months, called getmonths, since I am inexpert, I go through each month manually:

              <?php>
              $tpl = 'mynewsmonthrow';
              //Pass parent id as $docid in snippet call
              $resource = $modx->getObject('modResource',$docid);
              $year = $resource->get('pagetitle');
              $startparent = '<li class="month">';
              $startchild = '<ul>';
              $end = '</ul></li>';
              //Dec
              $month = 'December';
              $from = $year.'-12-00';
              $to = $year.'-12-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getdec = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getdec) {   $o .=  $startparent . $month . $startchild . $getdec . $end; }
              
              //Nov
              $month = 'November';
              $from = $year.'-11-00';
              $to = $year.'-11-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getnov = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getnov) {   $o .=  $start . $month . $getnov . $end; }
              
              //Oct
              $month = 'October';
              $from = $year.'-10-00';
              $to = $year.'-10-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getoct = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getoct) {   $o .=  $start . $month . $getoct . $end; }
              
              //Sep
              $month = 'September';
              $from = $year.'-09-00';
              $to = $year.'-09-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getsep = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getsep) {   $o .=  $start . $month . $getsep . $end; }
              
              //Aug
              $month = 'August';
              $from = $year.'-08-00';
              $to = $year.'-08-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getaug = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getaug) {   $o .=  $start . $month . $getaug . $end;  }
              
              //Jul
              $month = 'July';
              $from = $year.'-07-00';
              $to = $year.'-07-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getjul = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getjul) {   $o .=  $start . $month . $getjul . $end;  }
              
              //Jun
              $month = 'June';
              $month = 'June';
              $from = $year.'-06-00';
              $to = $year.'-06-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getjun = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getjun) {   $o .=  $o .=  $startparent . $month . $startchild . $getjun . $end;  }
              
              //May
              $month = 'May';
              $from = $year.'-05-00';
              $to = $year.'-05-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getmay = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getmay) {   $o .=  $start . $month . $getmay . $end;  }
              
              //Apr
              $month = 'April';
              $from = $year.'-04-00';
              $to = $year.'-04-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getapr = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getapr) {   $o .=  $start . $month . $getapr . $end;  }
              
              //Mar
              $month = 'March';
              $from = $year.'-03-00';
              $to = $year.'-03-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getmar = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getmar) {   $o .=  $start . $month . $getmar . $end; }
              
              //Feb
              $month = 'February';
              $from = $year.'-02-00';
              $to = $year.'-02-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getfeb = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getfeb) {   $o .=  $start . $month . $getfeb . $end; }
              
              //Jan
              $month = 'January';
              $from = $year.'-01-00';
              $to = $year.'-01-31';
              
              $tvfilter = 'date>>'.$from.',date<<'.$to;
              $getjan = $modx->runSnippet('getResources',array(
                 'parents' => $docid,
                 'tpl' => $tpl,
                 'limit' => '90',
                 'depth' => '0',
                 'showHidden' => 0,
                 'includeContent' => 0,
                 'includeTVs' => 1,
                 'processTVs' => 1,
                 'tvPrefix' => '',
                 'sortbyTV' => 'date',
                 'tvFilters' => $tvfilter,
                'sortdirTV' => 'ASC'
              ));
              if ($getjan) {   $o .=  $start . $month . $getjan . $end; }
              if($o) {
               $o = '<ul>' . $o . '</ul>';
               
               return $o;   } else {   echo "<p>No children found.</p>";  }


              That requires another chunk called mymonthnewsrow:

              <li><a href="[[+newsphoto]]" class="lbox noborder" title="[[+pagetitle]]">[[+pagetitle]]</a></li> 


              The news items have been turned into jpegs for some reason, and I am using a lightbox to show them as popups.

              Doubtless there is a far, far better way to do it.

              Thank you, again. [ed. note: cottagestuff last edited this post 6 years, 7 months ago.]
                • 3749
                • 24,544 Posts
                Would it work to use the createdon field or the resource? That would be a lot faster than using a TV. Another option would be to put the date in an unused resource field if there is one (e.g., description, introtext, longtitle).

                If you use the TV, I would do this with a custom snippet along these lines (untested), which should produce the whole thing, though you'll have to play around with the HTML to get it right:

                [[!getNews? &startYear=`2012` &tpl=`mynewsmonthrow`]]
                

                /* getNews snippet */
                
                $startYear = $modx->getOption('startYear' . $scriptProperties);
                $tpl = $modx->getOption('tpl' . $scriptProperties);
                $parents = $modx->resource->get('id');
                $currentYear = date("Y");
                $tvId = 999; // !Change this to the ID of the date TV
                
                $output = "";
                for ($year = $currentYear; $year >= $startYear; $year--) {
                    $output .= "\n<h3>$year</h3>\n";
                    $c = $modx->newQuery('modTemplateVarResource');
                    $c->where(array(
                        'tmplvarid' => $tvId,
                        'value:>=' => $year . '-1-1' ,
                        'value:<' => $year + 1 . '-1-1' 
                    ));
                    
                    $count = $modx->getCount($c);
                    
                    $output .= "\n<ul " . 'class=\"year\"\n>';
                    if ($count == 0) {
                        $output .= 'No News Found';
                        continue; // got to next year
                    }
                   
                   for ($month = 1; $month <=12; $month++) {
                       $lastDay = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                       $output .= '<h4>' . $month . '</h4>';
                
                       $c = $modx->newQuery('modTemplateVarResource');
                       $c->where(array(
                           'tmplvarid' => $tvId,
                           'value:>=' => $year . '-' . $month . '-1',
                           'value:<=' => $year . '-' . $month . '-' . $lastDay,
                       ));
                
                       $count = $modx->getCount($c);
                        
                       if ($count == 0) {
                           $output .= 'No News Found';
                           continue;
                       }
                        
                       $from = $year . '-' . $month . '1';
                       $to = $year . '-' . $month . '-' . $lastDay;
                       $tvFilter =  'date>>'.$from.',date<<'.$to;
                
                      $output .= $modx->runSnippet('getResources',array(
                          'parents' => $parents,
                          'tpl' => $tpl,
                          'limit' => '90',
                          'depth' => '0',
                          'showHidden' => 0,
                          'includeContent' => 0,
                          'includeTVs' => 1,
                          'processTVs' => 1,
                          'tvPrefix' => '',
                          'sortbyTV' => 'date',
                         'tvFilters' => $tvFilter,
                         'sortdirTV' => 'ASC'
                      ));
                   }
                   $output .= "\n</ul>\n";
                }
                
                return $output;
                





                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting