Limiting Search To Specific Category 1.0

June 4, 2003 - updated February 8, 2006

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.

  1. Open the file Search.pm in the lib/MT/App directory.
  2. Find the line (line 242 in MT 2.64) that says:
        my $iter = MT::Entry->load_iter(\%terms, \%args);
  3. 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>