We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    Turns out it was fairly easy to adapt getResourceTag for this purpose.

    In your template:
    [[!getRelated? &parents=`6` &tpl=`tagTpl` &tagKey=`my_tags` &tags=`[[*my_tags]]`]]
    


    And the snippet code (getRelated):
    <?php
    /**
     * getRelated
     * Based on getResourcesTag by Shaun McCormick
     *
    */
    
    $output = '';
    
    $tagKey = $modx->getOption('tagKey',$scriptProperties,'tags');
    $tags = $modx->getOption('tags',$scriptProperties,'');
    
    if (!empty($tags)) {
    
    	// explode tags into array
    	$tags = $modx->stripTags($tags);
    	$tags = explode(',',$tags);
    	
    	// convert to tvFilters
    	$tvFilters = array();
    	
    	foreach ($tags as $tag) {
    		$tvFilters[] = $tagKey.'==%'.$tag.'%';
    	}
    	
    	$scriptProperties['tvFilters'] = implode($tvFilters,'||');	
    		
    	// send to getResources
    	$elementObj = $modx->getObject('modSnippet', array('name' => 'getResources'));
    	if ($elementObj) {
    	    $elementObj->setCacheable(false);
    	    $output = $elementObj->process($scriptProperties);
    	} else {
    	    return 'You must have getResources downloaded and installed to use this snippet.';
    	}
    
    }	
    
    return $output;
    


    The snippet acts as a wrapper for getResources so you can use all properties available for that snippet with this one too.
    To exclude the current document from results: &resources=`-[[*id]]`

    Note that tags are searched using the ’or’ operator, so if the current document’s tags are ’pig, horse, cow’ it will return a list of any documents including the tags ’pig’ or ’horse’ or ’cow’.

    Let me know if it works for you...
      • 20358
      • 10 Posts
      Oh, that’s really cool. I’ll try that one out myself later this evening. The more I pondered the original request in this thread, the more I realized a "Related Posts" widgit would be pretty useful (and cool).

      Swill
        • 20358
        • 10 Posts
        @lucas: I tested this and it works, but there are a couple of weird quirks:

        1) If the currently selected resource has *no* tags, the getRelated resource list pulls up all resources *that have tags*. The result is a bit strange, but if all of your resources are tagged, then this is a non-issue.

        2) If the currently selected resource has *multiple* tags, the getRelated resource list pulls up ALL resources under the parent included in the &parent argument, regardless of whether or not they even have tags.

        Swill

          • 33968
          • 863 Posts
          Thanks for the feedback - I’ve updated the code in my post above to address the first ’empty tags’ issue. It will return nothing if there are no tags in the current resource.

          As for the multiple tags issue, I tested that quite a lot and again after seeing your post and I can’t reproduce it. It works as expected for me. The ’tags’ parameter is expecting a comma delimited list of tags - are you perhaps using a different delimiter? Could you tell me exactly what you have in [[*my_tags]]?

          Cheers
            • 21122
            • 153 Posts
            Just given this a whirl Lucas - Looks like it works!

            Love the fact that you can exclude documents if necessary too.

            Thanks for this, excellent stuff!
              • 33968
              • 863 Posts
              @neilp123: Glad you found it useful smiley

              How about with multiple tags on the currently displayed document (as per Mr Swillis feedback above)? Could you let me know if that works for you?
                • 21122
                • 153 Posts
                Hmmm - on closer inspection, it does appear that when a resource has multiple tags, relatedPages displays all of the resources within the parent ID.

                Wonder if there’s a fix?
                  • 33968
                  • 863 Posts
                  I can fix it if I understand what’s going wrong huh Multiple tags work perfectly on my test site.

                  Could you paste your whole snippet call for me so I can see if there’s anything might be causing that? As above, are your tags comma delimited?
                    • 21122
                    • 153 Posts
                    I certainly can smiley

                    Snippet call is:

                    [[!getRelated? &parents=`55,56,57,58` &tpl=`relatedPagesTpl` &tagKey=`tags` &tags=`[[*tags]]`]]


                    Auto Tag TV is call ’tags’. Want to pull articles from containers 55, 56, 57 and 58. relatedPagesTpl is:

                    <a href="[[~[[+id]]]]" title="[[+pagetitle]]">[[+pagetitle]]</a>


                    Hope that helps!
                      • 33968
                      • 863 Posts
                      Ahh... I’m afraid the issue is due to a bug in getResources related to multiple filters undecided

                      It works on my site as I’d applied one of the temporary fixes suggested, which I’ve now realised causes other problems. That’s why I wouldn’t suggest you do the same at this time. Sorry guys, may have to wait until this bug has been ironed out!