我找到了一种方法来实现这一点。我的想法是对的pre_get_posts
就是要走的路。首先,我添加了一个操作:
// Filter the search page
add_filter(\'pre_get_posts\', \'search_pre_get_posts\');
然后我添加了函数。此函数首先检查上下文是否正确。在自定义搜索表单中,我使用nonce。nonce帮助我验证正在运行的是我的自定义搜索,而不是内置搜索。然后,我操作将发送到查询的查询变量。我在这里展示了我的全部功能,其中包括所有变量的验证和清理,以及如何操作查询:
/**
* Filters for search of events
*
* When a search is initiated, this filter will check to see if it is an
* event search. If so, it will process the search data associated with
* event searches and add it to the $query object prior to it being used
* to query the database.
*
* @param $query The query object generated for this page
* @return void
*/
function search_pre_get_posts($query)
{
// Verify that we are on the search page that that this came from the event search form
if($query->query_vars[\'s\'] != \'\' && is_search() && isset($_GET[\'prefix-submitted\']) && $_GET[\'prefix-submitted\'] && wp_verify_nonce($_GET[\'prefix-search-nonce\'], \'prefix-search\'))
{
// Always order by date
$query->set(\'orderby\', \'meta_value\');
$query->set(\'order\', \'ASC\');
$query->set(\'meta_key\', \'_datetime-from\');
// Set the text string for the search
$clean_text = (isset($_GET[\'prefix-text\'])) ? wp_strip_all_tags($_GET[\'prefix-text\']) : \'\';
$query->set(\'s\', $clean_text);
// Validate dates
$clean_from = (isset($_GET[\'prefix-from\'])) ? $this->handle_date($_GET[\'prefix-from\']) : false;
$clean_to = (isset($_GET[\'prefix-to\'])) ? $this->handle_date($_GET[\'prefix-to\']) : false;
// Validate taxonomies
$clean_type = (isset($_GET[\'prefix-type\'])) ? $this->handle_taxonomy_id($_GET[\'prefix-type\'], \'prefix-type\') : false;
$clean_location = (isset($_GET[\'prefix-location\'])) ? $this->handle_taxonomy_id($_GET[\'prefix-location\'], \'prefix-location\') : false;
// Build meta_query variable based on date pieces
if($clean_from && $clean_to)
{
$meta_query = array(
array(
\'key\' => \'_datetime-from\',
\'value\' => array($clean_from, $clean_to),
\'compare\' => \'BETWEEN\',
\'type\' => \'DATE\'
)
);
}
elseif($clean_from)
{
$meta_query = array(
array(
\'key\' => \'_datetime-from\',
\'value\' => $clean_from,
\'compare\' => \'>=\',
\'type\' => \'DATE\'
)
);
}
elseif($clean_to)
{
$meta_query = array(
array(
\'key\' => \'_datetime-from\',
\'value\' => $clean_from,
\'compare\' => \'>=\',
\'type\' => \'DATE\'
)
);
}
else
$meta_query = \'\';
// Set the meta query
$query->set(\'meta_query\', $meta_query);
// Build tax query based on taxonomy pieces
if($clean_type && $clean_location)
{
$tax_query = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'prefix-type\',
\'field\' => \'id\',
\'terms\' => $clean_type,
\'operator\' => \'IN\'
),
array(
\'taxonomy\' => \'prefix-location\',
\'field\' => \'id\',
\'terms\' => $clean_location,
\'operator\' => \'IN\'
)
);
}
elseif($clean_type)
{
$tax_query = array(
array(
\'taxonomy\' => \'prefix-type\',
\'field\' => \'id\',
\'terms\' => $clean_type,
\'operator\' => \'IN\'
)
);
}
elseif($clean_location)
{
$tax_query = array(
array(
\'taxonomy\' => \'prefix-location\',
\'field\' => \'id\',
\'terms\' => $clean_location,
\'operator\' => \'IN\'
)
);
}
else
$tax_query = \'\';
// Set the tax query
$query->set(\'tax_query\', $tax_query);
}
}