June 04, 2003
MovableType allows you to limit your search to a specific blog, but does not allow you to limit the search to a specific category. This hack enables you to specify which category you want to search.
lib/MT/App/Search.pm Modification
This modification allows you to specify which category is searched.
- Open the file Search.pm in the lib/MT/App directory.
- Find the line (line 242 in MT 2.64) that says:
my $iter = MT::Entry->load_iter(\%terms, \%args);
- Insert the following code before it:
# BEGIN Category Search HACK
# http://www.nonplus.net/software/mt/LimitingSearchToSpecificCategory.htm
if(my $cat = $app->{query}->param('CategoryID')) {
$args{'join'} = [ 'MT::Placement', 'entry_id',
{ category_id => $cat }, { 'unique' => 1 } ];
}
# END Category Search HACK
Modifying your Search Form
After you modify the Search.pm file, you can specify which category to search by modifying your search form to include a CategoryID control that specifies which category is to be seached.
To specify that the current category is to be searched, add the following to your search form in a category template:
<input type="hidden" name="CategoryID" value="<$MTCategoryID$>">
To allow the user to select which category to search from a menu, add the following to the search form in an index template or search template:
<select name="CategoryID">
<option value="0">All Categories</option>
<MTCategories>
<option value="<$MTCategoryID$>"><$MTCategoryLabel$></option>
</MTCategories>
</select>