We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17410
    • 13 Posts
    I’m trying to show popular articles from a certain Container/Parent. I got PageHitCountResults working, but the only way I can figure out how to exclude other pages is to type in every single pageID in the &excludeIDs, which can be a problem if I’ve got hundreds of pages.

    Any ideas?

    Thanks.
      • 4310
      • 2,310 Posts
      Well, it could be changed to only include a comma separated list of id’s or parent id’s.
      Or maybe only include/exclude certain templates, or if a certain tv has a value.
      Depends on what your specific needs are.
        • 17410
        • 13 Posts
        Having it only include a certain parent and it’s children would be perfect. I’m going to try to figure it out on my own, but if anyone already has it written and cares to share, that would be great.

        Thanks.
          • 4310
          • 2,310 Posts
          Here’s a quick, dirty and untested modification to take an extra parameter of &parent=`**` where ** is the parent doc id.
          <?php
          $table_prefix = (isset($table_prefix)) ? $table_prefix : 'modx_';
          $limit = (isset($limit)) ? $limit : 5;
          $excludeIDs = (isset($excludeIDs)) ? $excludeIDs : '0';
          $tpl = (isset($tpl)) ? $tpl : 'top_pages';
          $output = '';
          $sql = mysql_query('SELECT * FROM `'.$table_prefix.'page_hit_counter`, `'.$table_prefix.'site_content` WHERE '.$table_prefix.'page_hit_counter.page_id='.$table_prefix.'site_content.id AND '.$table_prefix.'site_content.id NOT IN('.$excludeIDs.') AND '.$table_prefix.'site_content.parent = '.$parent.' ORDER BY `page_count` DESC LIMIT '.$limit.'');
          $resultArray = $modx->db->makeArray( $sql );
          foreach($resultArray as $item)
          {
          $params['phc.count']=$item['page_count'];
          $params['phc.pageID']=$item['page_id'];
          $params['phc.title']=$item['pagetitle'];
          $params['phc.longtitle']=$item['longtitle'];
          $params['phc.description']=$item['description'];
          $output.=$modx->parseChunk(''.$tpl.'', $params, '[+', '+]');
          }
          return $output;
          ?>
            • 813 ☆ A M B ☆
            • 326 Posts
            Thanks for supplying the code for parent items only -- I can verify that it works. What would I need to change to make this work with grandparents? In other words, the pages I wanted counted are within subdirectories, and all of those subdirectories are within the doc ID I’d like to reference in this snippet.

            Thanks!
              • 4310
              • 2,310 Posts
              Here you go.
              &top_parent is for the Doc ID of parent, grandparent etc.
              &level is for how many levels down you want to go from the top parent
              <?php
              $table_prefix = (isset($table_prefix)) ? $table_prefix : 'modx_';
              $limit = (isset($limit)) ? $limit : 25;
              $top_parent = (isset($top_parent)) ? $top_parent : 0;
              $level = (isset($level)) ? $level : 2;
              $excludeIDs = (isset($excludeIDs)) ? $excludeIDs : 0;
              $tpl = (isset($tpl)) ? $tpl : 'top_pages';
              $children = $modx->getChildIds($top_parent, $level);
              $child_ids = implode(",", $children);
              $output = '';
              $sql = mysql_query('SELECT * FROM `'.$table_prefix.'page_hit_counter`, `'.$table_prefix.'site_content` WHERE '.$table_prefix.'page_hit_counter.page_id='.$table_prefix.'site_content.id AND '.$table_prefix.'site_content.id IN('.$child_ids.') AND '.$table_prefix.'site_content.id NOT IN('.$excludeIDs.') ORDER BY `page_count` DESC LIMIT '.$limit.'');
              $resultArray = $modx->db->makeArray( $sql );
              foreach($resultArray as $item)
              {
              $params['phc.count']=$item['page_count'];
              $params['phc.pageID']=$item['page_id'];
              $params['phc.title']=$item['pagetitle'];
              $params['phc.longtitle']=$item['longtitle'];
              $params['phc.description']=$item['description'];
              $output.=$modx->parseChunk(''.$tpl.'', $params, '[+', '+]');
              }
              return $output;
              ?>
                • 813 ☆ A M B ☆
                • 326 Posts
                That works great! Thank you so much. In fact, it taught me how to use child IDs in a way I hadn’t understood before. I can’t thank you enough!!! These forums have saved my butt more times than I can count.
                  • 4310
                  • 2,310 Posts
                  I charge extra for tuition laugh
                    • 813 ☆ A M B ☆
                    • 326 Posts
                    Haha, the check is in the mail.
                      • 2198
                      • 7 Posts
                      Hi this second solution help me fine. I want to add also date to result. I have tried in stead of description to add post_date variable. But doesn’t work. Could you help me?