April 8th, 2015 · php modx

MODX: Combining AdvSearch and Tagger

A recent client project took me down the path of integrating AdvSearch and Tagger with the goal of allowing users to search for a page by tag. It took much digging (including a trip to the Wayback Machine for some lost AdvSearch documentation) but this is the solution I came up with:

Once you have both AdvSearch and Tagger installed and working, all you need to do is create this MODX snippet:

<?php

$modxTablePrefix = 'modx_';
$taggerModelPath = '{core_path}components/tagger/model/';

$hook->setQueryHook(array(
    'qhVersion' => '1.2',
    'joined' => array(
        array(
            'package' => 'tagger',
            'class' => 'TaggerTagResource',
            'packagePath' => $taggerModelPath,
            'withFields' => 'resource',
            'tablePrefix' => $modxTablePrefix,
            'joinCriteria' => 'TaggerTagResource.resource = modResource.id'
        ),
        array(
            'package' => 'tagger',
            'class' => 'TaggerTag',
            'packagePath' => $taggerModelPath,
            'withFields' => 'tag',
            'tablePrefix' => $modxTablePrefix,
            'joinCriteria' => 'TaggerTag.id = TaggerTagResource.tag'
        ),
    )
));

return true;

Then add it to your AdvSearch call as a queryHook:

[[!AdvSearch? &queryHook=`your_snippet_name_here`]]

...and, voila, any pages which have tags matching a search term will now show up in the AdvSearch search results!