Exclude resources from TaggerGetResourcesWhere

On a blog/news page I want to show related articles using the Tagger extra. (ModX 2.7.3, newest versions of extra’s)
So I have taggerGetTags fetch the tags for the current resource in taggerGetResourcesWhere in pdoResources tags as follows:
1248 is the id of the newspages container.

[[!pdoResources?
    &parents=`1248`
    &exclude=`[[*id]]`
    &depth=`1`
    &where=
    `[[!TaggerGetResourcesWhere?
        &tags=`[[!TaggerGetTags? &resources=`[[*id]]` &rowTpl=`tag` &separator=`,` &outTpl=`tags`]]`
     ]]`
     &tpl=`blogTextImgRelatedBlocks`
     &sortby=`RAND()` 
     &showLog=`0`
]]

This shows articles with similar tags, that’s working ok. But I don’t want to show the current article in the related articles.
I tried
&parents=1248, -[[*id]], &resources=-[[*id]], &exclude=-[[*id]]
all not working, ie. the current article is not excluded.

How can I achieve this?

The line

&resources=-[[*id]]

should normally work, but it seems that with the use of

&where=`[[!TaggerGetResourcesWhere? ... ]]`

the condition to exclude the resource gets deleted (somewhere here in the code).

Probably the easiest way for you to get the desired result is therefore to make a change to the snippet TaggerGetResourcesWhere:
Duplicate the snippet and give it a new name like TaggerGetResourcesWhereExcludeCurrentResource. Then add this line at the end of the snippet code right before the return- statement.

$where[] = "`modResource`.`id` <> " . $modx->resource->get('id');

Now use this new snippet in your call to pdoResources:

[[!pdoResources?
    &parents=`1248`
    &depth=`1`
    &where=
    `[[!TaggerGetResourcesWhereExcludeCurrentResource?
        &tags=`[[!TaggerGetTags? &resources=`[[*id]]` &rowTpl=`tag` &separator=`,` &outTpl=`tags`]]`
     ]]`
     &tpl=`blogTextImgRelatedBlocks`
     &sortby=`RAND()` 
     &showLog=`0`
]]
1 Like

maybe TaggerGetRelatedWhere would do what you want (get related resources, but not the current one)

2 Likes

It seems that it isn’t necessary to duplicate the snippet TaggerGetResourcesWhere. If you just provide a &where-property it should work too:

[[!pdoResources?
    &parents=`1248`
    &depth=`1`
    &where=
    `[[!TaggerGetResourcesWhere?
        &tags=`[[!TaggerGetTags? &resources=`[[*id]]` &rowTpl=`tag` &separator=`,` &outTpl=`tags`]]`
		&where=`{"id:!=":[[*id]]}`
     ]]`
     &tpl=`blogTextImgRelatedBlocks`
     &sortby=`RAND()` 
     &showLog=`0`
]]

But Bruno’s solution (the use of TaggerGetRelatedWhere) appears to do exactly the same and is more elegant.

1 Like

@bruno17 Thank you! and @halftrainedharry too of course for answering.
TaggerGetRelatedWhere does exactly what I need, moreover it doesn’t return random results when the current resource has no tags, which was another problem I had to tackle.
This snippet should definitely be added to the docs on docs.modx.com.