We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Trying to figure this one out... I want a small call-out that has the most recent [top 5 or so] and the most viewed [5 or so] resources listed.... isn’t there a simple way to do this? I’m looking at get resources, but can’t seem to figure it out. It looks like it might be able to get the most recent [by edit/publish date] but most viewed??

    Before I go to the effort of creating a TV that will store page views, is this functionality already in there...

    And how can I filter the getResources to display only resources that use a specific template [i.e. I don’t want it finding my ’landing pages’ as the most recent.]

    -thanks
    -sean
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 26503
      • 620 Posts
      Got the ’most recent document worked out.

      
       [[!getResources?  
          &parents=`0` 
          &tpl=`MostRecentDocs`  
          &limit=`5`  
          &sortdir=`ASC`  
          &includeTVs=`0`  
          &includeContent=`0`   
          &depth=`9999`  
          &sortby=`{"editedon":"DESC","publishedon":"DESC","createdon":"DESC"}` 
          &where=`{"template:!=":2}` 
          ]] 
      


      Just goes to show what you can do if you actually read the docs!!

      Now for the "most popular pages"

      Somehow, I don’t think this will be quite so easy... anyone done this before?

      -thanks
      -sean
        *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

        Sean Kimball CLP, CLS.
        Technical Director / Sr. Developer | BigBlock Studios
        ._______________________________________________.
        Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
        27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
        phone/fax: 905-426-5525
        • 3749
        • 24,544 Posts
        I haven’t done it, but it should be possible with a TV to store the page count and a snippet to update its value.

        I think you could get the results using &tvFilters for getResources.
          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
          • 26503
          • 620 Posts
          Been working on this, [yes it’s possible]

          I created a plugin that fires on the ’onWebPageComplete’ event, a TV called ’ViewCount’ numeric type with a default of zero on all templates:

          Then this code went into the plugin"

          <?php
          $docID = $modx->resource->get('id');
          $doc=$modx->resource;
          
          $tv = $modx->getObject('modTemplateVar',array('name'=>'ViewCount'));
          
          $oldValue= $modx->getObject('modResource',$docID );
          //echo $oldValue;
          $newValue = $oldValue->getTVValue('ViewCount') + 1;
          //echo $newValue ;
          $tv->setValue($docID, $newValue);
          $tv->save();
          $doc->save();
          
          


          feels messy, but it works... the clues were in this thread: http://modxcms.com/forums/index.php?topic=52849.0

          I’ll update when I figure out how to get [[getResources]] to sort on the ViewCount TV.

          -thanks
          -sean

          **UPDATE**

          that was easy:

          [[!getResources?  
          	&parents=`0` 
          	&tpl=`MostRecentDocs`  
          	&limit=`5`  
          	&includeTVs=`1`  
          	&includeContent=`0`   
          	&depth=`9999`  
          	&sortbyTV=`ViewCount`
          	&sortdirTV=`DESC`
          	&where=`{"template:!=":2}` 
          	]]  
          
            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 3749
            • 24,544 Posts
            Looks good. BTW, there’s no need to get or save the resource. TV values are stored in a separate table.
              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
              • 14437
              • 31 Posts
              It's working for me! though not every page reload is saved/counted.
              Is this because of cache/cookies?
                • 3749
                • 24,544 Posts
                I'm not sure. It should fire on every request, but if the resource is cached, you might see the old value of the TV. Be sure the TV tag in the page is uncached: [[!*ViewCount]]. You might also try connecting it to OnWebPagePrerender instead, though it shouldn't mater.

                I think your call to getTVValue() is incorrect. It's argument is just the name or ID of the TV..

                This would be faster and (I think) more reliable:

                <?php
                 
                $tv = $modx->getObject('modTemplateVar',array('name'=>'ViewCount'));
                $tvId = $tv->get('id');
                 
                $oldValue= $modx->resource->getTVValue($tvId);
                //echo $oldValue;
                $newValue = $oldValue + 1;
                //echo $newValue ;
                $tv->setValue($modx->resource->get('id'), $newValue);
                $tv->save();
                
                  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