We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52499
    • 6 Posts
    Hey I am trying to get the count for blog posts within a collection however I want to be more specific.
    I want to be able to count the children but only ones that tag (template variable) match a certain one.

    I have this code that will return the count of the whole collection:

    <?php
    $count = 0;
    $parent = isset($parent) ? (integer) $parent : 0;
    if ($parent > 0) {
        $criteria = array(
            'parent' => $parent,
            'deleted' => false,
            'published' => true,
        );
        $count = $modx->getCount('modResource', $criteria);
    }
    return (string) $count;
    


    But i would like to add to the $criteria array something like " 'tv.filterTag' => 'blogTag1' "

    Is this possible?


    Thank you in advance
    Ryan
      • 3749
      • 24,544 Posts
      If your tag TV does not use @INHERIT and doesn't have a default value (which will make things significantly faster), you can do this (untested):

      $tvId = 12; /* Change this to the ID of your tag TV */
      $searchTag = 'blog1;
      
      $c = array(
          'tmplvarid' => $tvId, 
          'value' => $searchTag,
      );
      
      $count = $modx->getCount('modTemplateVarTemplate', $c);


      This assumes that you can count on an exact match for the tag and the search term. If the user is entering the search term, you'll want to use trim() on it (and sanitize it!!). [ed. note: BobRay last edited this post 7 years, 9 months ago.]
        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