如果不修改wordpress生成的主搜索查询,就没有简单的方法可以做到这一点。我想你应该posts_search
&;posts_join
钩它们位于wp-includes/query.php
执行var_dump
首先查看您得到了什么&;相应修改。这里有一些未经测试的代码,我现在可以为您提供这些代码。
add_filter(\'posts_search\', \'search_function\', 10, 2);
function search_function($search, $query) {
if(is_admin() || !$query->is_main_query() || !$query->is_search)
return; //determine if we are modifying the right query
global $wpdb;
$search_term = $query->get(\'s\');
$search = \' AND (\';
//point 1
$search .= "($wpdb->posts.post_content LIKE \'%$search_term%\')";
//need to add an OR between search conditions
$search .= " OR ";
//point 2
$search .= "($wpdb->comments.comment_content LIKE \'%$search_term%\')";
//need to add an OR between search conditions
$search .= " OR ";
//point 3
$search .= "($wpdb->postmeta.meta_key = \'custom_field_key\' AND $wpdb->postmeta.meta_value LIKE \'%$search_term%\')";
//need to add an OR between search conditions
$search .= " OR ";
//point 4
$search .= "({$wpdb->prefix}notes.text LIKE \'%$search_term%\')";
//add the filter to join, sql will error out without joining the tables to the query
add_filter(\'posts_join\', \'join_tables\');
return $search . \') \';
}
function join_tables($join) {
$join .= "JOIN $wpdb->comments ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)";
$join .= "JOIN $wpdb->postmeta ON ($wpdb->postmeta.post_ID = $wpdb->posts.ID)";
$join .= "JOIN {$wpdb->prefix}notes ON ({$wpdb->prefix}notes.post_ID = $wpdb->posts.ID)";
return $join;
}