默认搜索查询在多个表上进行wordpress搜索

时间:2013-06-02 13:45:20

标签: database wordpress preg-replace

至少在MySQL噩梦中度过几天希望有人可以帮助我。我正在研究WP的标记插件。

我的问题听起来很容易:为了保持wordpress数据库的清洁和高效,我决定在一些额外的表中保存其他字段(标签,标题,自由文本,网址,...)而不添加典型的元字段。现在,搜索查询也必须包含这些字段。

几小时后我发现(geoteagging wordpress example):

add_filter('posts_join', 'geotag_search_join' );
add_filter('posts_where', 'geotag_search_where' );
add_filter('posts_groupby', 'geotag_search_groupby' );
到目前为止一切顺利。现在是时候重写查询(geotag_search_where),但这里的噩梦开始......

自定义表名是:“abc_tags”和名为“abc_title”和“abc_tags”的字段。 “abc_tags”是一个长文本字段,以逗号分隔的纯文本项。

我不理解使用查询的preg_replace操作...我希望有人可以提供帮助。

function geotag_search_where( $where )
{
  if( is_search() ) {
    $where = preg_replace(
       "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
       "(post_title LIKE $1) OR (geotag_city LIKE $1) OR (geotag_state LIKE $1) OR (geotag_country LIKE $1)", $where );
   }

  return $where;
}

感谢。

1 个答案:

答案 0 :(得分:0)

他们在搜索$where中使用SQL字符串,而不是只搜索post_title变量,他们将附加到MySQL查询字符串中geotag_citygeotag_stategeotag_country交叉检查其他匹配。

这样,如果您搜索“New York”,它将返回geotag_city匹配的结果以及标题。 (列出标题中每个可能的关键字都不是好的形式。)