如果可能的话,可以这样做:
$searchquery = new WP_Query( array(
\'s\' => \'hello \'
));
因为所有查询归结为
WP_Query
在最基本的层面上,我们可以通过查看
wp-includes/query.php
// If a search pattern is specified, load the posts that match
if ( !empty($q[\'s\']) ) {
// added slashes screw with quote grouping when done early, so done later
$q[\'s\'] = stripslashes($q[\'s\']);
if ( !empty($q[\'sentence\']) ) {
$q[\'search_terms\'] = array($q[\'s\']);
} else {
preg_match_all(\'/".*?("|$)|((?<=[\\r\\n\\t ",+])|^)[^\\r\\n\\t ",+]+/\', $q[\'s\'], $matches);
$q[\'search_terms\'] = array_map(\'_search_terms_tidy\', $matches[0]);
}
$n = !empty($q[\'exact\']) ? \'\' : \'%\';
$searchand = \'\';
foreach( (array) $q[\'search_terms\'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}(($wpdb->posts.post_title LIKE \'{$n}{$term}{$n}\') OR ($wpdb->posts.post_content LIKE \'{$n}{$term}{$n}\'))";
$searchand = \' AND \';
}
if ( !empty($search) ) {
$search = " AND ({$search}) ";
if ( !is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = \'\') ";
}
}
// Allow plugins to contextually add/remove/modify the search section of the database query
$search = apply_filters_ref_array(\'posts_search\', array( $search, &$this ) );
由此我们可以确定,在多个阶段,搜索字符串被剥离和清理,任何额外的空间都将作为清理过程的一部分被删除。
进行此类搜索的唯一方法是使用posts_search
过滤以使用您自己的卫生过程复制SQL,但我strong 出于安全和维护原因,建议不要这样做。我建议,无论您拥有什么样的系统,重新构建它都比沿着这条路走容易,而且可以认为这样做是作为开发人员不负责任的,而且非常重要。
相反,我建议您使用专用的SQL工具(如workbench)和类似以下的查询进行搜索:
SELECT * FROM wp_posts WHERE post_content LIKE "hello " LIMIT 0,10