我被难住了。我已经做了大量的工作来过滤在WP搜索中得到的帖子,然后在从DB中得到这些帖子后对它们进行排序。我现在的主要问题是获得与标记匹配的结果。我只是直接运行SQL查询并检查输出,而不是通过WP反复运行它。
首先是WP生成的搜索查询(我添加了过滤器,并扩展了重要部分):
SELECT SQL_CALC_FOUND_ROWS
wp_3_posts.ID FROM wp_3_posts
LEFT JOIN wp_3_term_relationships AS trel ON (wp_3_posts.ID = trel.object_id)
LEFT JOIN wp_3_term_taxonomy AS ttax ON ( ( ttax.taxonomy = \'post_tag\' )
AND trel.term_taxonomy_id = ttax.term_taxonomy_id)
LEFT JOIN wp_3_terms AS tter ON (ttax.term_id = tter.term_id)
WHERE 1=1
AND
(
(
(
(
(wp_3_posts.post_title LIKE \'%neck%\')
OR
(wp_3_posts.post_content LIKE \'%neck%\')
)
AND
(
(wp_3_posts.post_title LIKE \'%pain%\')
OR
(wp_3_posts.post_content LIKE \'%pain%\')
)
)
OR
(
(
(tter.name LIKE \'%neck%\')
AND
(tter.name LIKE \'%pain%\')
)
OR
(tter.name LIKE \'%neck pain%\')
)
OR
(
(
(wp_3_posts.post_excerpt LIKE \'%neck%\')
AND
(wp_3_posts.post_excerpt LIKE \'%pain%\')
)
OR
(wp_3_posts.post_excerpt LIKE \'%neck pain%\')
)
)
)
AND (wp_3_posts.post_password = \'\')
AND wp_3_posts.post_type IN (\'post\', \'page\', \'attachment\', \'syndicated\')
AND (wp_3_posts.post_status = \'publish\')
ORDER BY wp_3_posts.post_date DESC LIMIT 0, 7
第一部分通过连接获取必要的表,这似乎很好。我正在运行的搜索是“颈部疼痛”,标题、内容或摘录中包含这些词的任何帖子都会按预期显示出来。我有一个贴子贴着“颈部”、“疼痛”和“颈部疼痛”。该帖子ID不会随此查询返回。让我抓狂的是,如果我删除这个查询中查看post\\u内容和post\\u标题的部分,我的标记文章就会显示出来。具体而言,如果我删除此项:
(((wp_3_posts.post_title LIKE \'%neck%\')
OR (wp_3_posts.post_content LIKE \'%neck%\'))
AND ((wp_3_posts.post_title LIKE \'%pain%\')
OR (wp_3_posts.post_content LIKE \'%pain%\')))
OR
这将我的查询保留为
SELECT SQL_CALC_FOUND_ROWS
wp_3_posts.ID FROM wp_3_posts
LEFT JOIN wp_3_term_relationships AS trel ON (wp_3_posts.ID = trel.object_id)
LEFT JOIN wp_3_term_taxonomy AS ttax ON ( ( ttax.taxonomy = \'post_tag\' )
AND trel.term_taxonomy_id = ttax.term_taxonomy_id)
LEFT JOIN wp_3_terms AS tter ON (ttax.term_id = tter.term_id)
WHERE 1=1
AND
(
(
(
(
(tter.name LIKE \'%neck%\')
AND
(tter.name LIKE \'%pain%\')
)
OR
(tter.name LIKE \'%neck pain%\')
)
OR
(
(
(wp_3_posts.post_excerpt LIKE \'%neck%\')
AND
(wp_3_posts.post_excerpt LIKE \'%pain%\')
)
OR
(wp_3_posts.post_excerpt LIKE \'%neck pain%\')
)
)
)
AND (wp_3_posts.post_password = \'\')
AND wp_3_posts.post_type IN (\'post\', \'page\', \'attachment\', \'syndicated\')
AND (wp_3_posts.post_status = \'publish\')
ORDER BY wp_3_posts.post_date DESC LIMIT 0, 7
也许这里有一些我对SQL不太了解的地方,我对它有点在行。任何智慧都将不胜感激,谢谢!