I am trying to customize the search results for a generic search, but only if it's using the search.php
template. I have other searches on my site, and I don't want this filter interfering with them. Everything works on the following code, except for is_page_template('search.php')
.
Once I enter that line of code as a condition, it just reverts to the normal search filtering, because my IF condition is failing. How do I only run my code if we're on the search.php
page?
//Filter the search for only posts and parts
function SearchFilter($query)
{
if ($query->is_search && is_page_template('search')) {
$query->set('post_type', array('post', 'parts'));
$query->set('meta_key', 'Manufacturer');
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
}
return $query;
}
add_filter('pre_get_posts', 'SearchFilter');
is_page_template
runs afterpre_get_posts
. That's why you can't use it yet.