SimpleSearch - Custom Hook to filter out older events by date

I’m using simplesearch to search the event resources. However, I only want events that are still upcoming and not displaying events with a date less than the current date. The TV to check is [[*event-date]].

I presume I need to use a custom hook for it but not sure how to start.
Current snippet calls are:

[[SimpleSearchForm? &tpl=`MySearchForm` &landing=`826`]]

[[!SimpleSearch? &tpl=`search-result-tpl` &ids=`172` &includeTVs=`1` &processTVs=`1` &perPage=`10`]]

I could be wrong, but I don’t think there is an easy way with SimpleSearch to filter the search.

  • The easiest way is probably to write a custom snippet for the &exclude property that creates a list of IDs of events that already took place.

  • There are also postHooks in SimpleSearch, but you basically have to write the whole query yourself. So it may be easier to just use pdoResources/pdoPage for the search instead, and create a custom snippet for the &where property.

  • You could also create your own driver class by copying and adjusting the class SimpleSearchDriverBasic. Then change the system settings simplesearch.driver_class and simplesearch.driver_class_path to use your custom driver.

What do you mean by “use pdoResources/pdoPage for the search instead”?
How would I use pdotools to do the search? Can I call SimpleSearch via PdoTools?

No.


You could do something like this:

[[!pdoResources?
    ...
    &includeTVs=`event-date`
    &where=`[[!getWhere]]`
]]

Snippet getWhere:

<?php
$where = [];
//read search parameter
$search_string = $_GET['search'] ?? ''; 

//do some sanitizing
$search_string = $modx->sanitizeString($search_string);
$search_string = strip_tags($search_string);
$search_string = trim($search_string);

if (mb_strlen($search_string) > 2){ //check min search length
    //filter by search term
    $where[] = ["pagetitle:LIKE" => "%" . $search_string . "%", "OR:content:LIKE" => "%" . $search_string . "%" ];
}

//filter by event-date
$where[] = ["event-date:>" => date('Y-m-d H:i:s')];

return json_encode($where);

Depending on your use case, something like this might be a viable replacement for SimpleSearch and is probably easier to implement than a custom postHook.

1 Like

So we can use pdoResources to display the SimpleSearch results?
How would I also include the usual template/folder filters in [[getWhere]]?

&where=`{ "template:=":32}`

Like this?

$where[] = ["event-date:>" => date('%Y-%m-%d'),"AND:template:=" => 32];
$where[] = ["event-date:>" => date('Y-m-d 00:00:00'), "AND:template:=" => 32];

should work, or also

$where[] = ["event-date:>" => date('Y-m-d 00:00:00')];
$where[] = ["template:=" => 32];

You don’t need SimpleSearch. Just a search form that sends a GET parameter search.

Amazing! Thanks for your help Harry!