Listing MODX Articles with at least one common tag

144 views Asked by At

Is it possible to use the getResources snippet to find related articles that have at least one common tag with the active resource?

[[getResources? 
        &parents=`xxx, xxx` 
        &showHidden=`1` 
        &limit=`10` 
        &tpl=`relatedArticle-tpl`  
        &depth=`1` 
        &includeContent=`1`
        &includeTVs=`1`
        &processTVs=`1`
        &sortby=`publishedon`
        &tvFilters=`something here` 
]]

The TV [[*articletags]] of the active resource should contain at least one common tag with a matching resource.

1

There are 1 answers

0
Niko Suominen On

This is the way how I implemented question:

[[getResources? 
    &parents=`x,x` 
    &showHidden=`1` 
    &limit=`10` 
    &tpl=`latestArticle`  
    &depth=`1` 
    &includeContent=`1`
    &includeTVs=`1`
    &resources=`-[[*id]]`
    &processTVs=`0`
    &sortby=`publishedon`
    &tvFilters=`[[tvFilterForRelatedArticles]]`
]]

And the snippet to make a TV filter:

$tags = $modx->resource->getTVValue('articlestags');
$categories = $modx->resource->getTVValue('category');
$tagsArray = explode(",", $tags);
$categoriesArray = explode(",", $categories);
$result = "";
foreach ($tagsArray as $tag) 
    $result .= "articlestags==%".$tag."%||";
foreach ($categoriesArray as $category) 
    $result .= "category==%".$category."%||";
return substr($result, 0, -1);

Not sure if this is the simplest way to achieve my target. Feel free to comment if there is a better solution.