WordPress查询post_title和meta_value搜索词
花了一天的时间在这上面,对于一个新手来说,不幸的是我在兜圈子。
假设我正在搜索“The Community Axminster”这个词。该术语中的单词应出现在 wp_posts 表中的 post_title 中,或者相关帖子的 wp_postmeta 表中的元键“rz_location__geo_city”的元值中。 需要注意的一点是,meta_key“rz_location__geo_city”并不总是针对每个 post_id 出现(Google 并不总是在 Places API 中返回位置)。
table: wp_posts
+------+----------------------------+-------------+------------+
| ID | post_title | post_status | post_type |
+------+----------------------------+-------------+------------+
| 9465 | Vila Anna | publish | rz_listing |
| 9291 | The Community Waffle House | publish | rz_listing |
+------+----------------------------+-------------+------------+
table: wp_postmeta
+---------+---------+-----------------------+------------+
| meta_id | post_id | meta_key | meta_value |
+---------+---------+-----------------------+------------+
| 29570 | 9465 | rz_is_claimed | 1 |
| 29571 | 9465 | rz_location__lat | 42.9875625 |
| 29572 | 9465 | rz_location__lng | 13.8987698 |
| 29573 | 9465 | rz_location__geo_city | Palma |
| 29577 | 9291 | rz_is_claimed | 1 |
| 29578 | 9291 | rz_location__lat | 11.8976543 |
| 29579 | 9291 | rz_location__lng | 14.9087654 |
| 2958 | 9291 | rz_location__geo_city | Axminster |
+---------+---------+-----------------------+------------+
我能想到的最好的办法是:
$posts = $wpdb->get_results(
$wpdb->prepare("
SELECT p.post_title, p.ID, p.post_type, p.post_status, m.post_id, m.meta_value, m.meta_key
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} m
ON ( p.ID = m.post_id )
WHERE p.post_type = %s
AND p.post_status = %s
AND ( ( p.post_title LIKE '%%$wordone%%' OR p.post_title LIKE '%%$wordtwo%%' ) OR ( m.meta_key = 'rz_location__geo_city' AND ( m.meta_value LIKE '%%$wordone%%' OR m.meta_value LIKE '%%$wordtwo%%' ) ) )
",
$post_type,
$post_status
)
);
这个请求的问题是,如果我输入单词“the”,我会返回同一篇文章的一组结果,但我期待一个结果。如果我搜索“社区 axminster”、“stackoverflow”也是如此。有趣的是,如果我搜索“axminster anna”,那么我会得到“The Community Waffle House”的一个结果以及一系列 Vila Anna 的结果,但实际上我应该返回零结果。如果我搜索“Palma Axminster”,我会得到两个结果,但我应该返回零。
非常感谢任何帮助。我不确定是否有必要这么说,但在上面的查询中,为了简单起见,我写了 p.post_title LIKE '%%$wordone%%' OR p.post_title LIKE '%%$wordtwo%%'
但实际上我正在使用搜索词并执行以下操作:
$words = explode(' ', $search_term);
$conds = array();
foreach ($words as $val) {
$conds[] = "p.post_title LIKE '%%".$val."%%'";
}
$implode_statement = implode(' OR ', $conds);
Spent a day on this and for a novice I'm going around in circles unfortunately.
Let's say I'm search for the word "The Community Axminster". The words in this term should appear in either the post_title in the wp_posts table, or the meta_value for the meta_key 'rz_location__geo_city' in the wp_postmeta table for the associated post. One thing to note is that the meta_key 'rz_location__geo_city' is not always present for each post_id (Google does not always return a location in the Places API).
table: wp_posts
+------+----------------------------+-------------+------------+
| ID | post_title | post_status | post_type |
+------+----------------------------+-------------+------------+
| 9465 | Vila Anna | publish | rz_listing |
| 9291 | The Community Waffle House | publish | rz_listing |
+------+----------------------------+-------------+------------+
table: wp_postmeta
+---------+---------+-----------------------+------------+
| meta_id | post_id | meta_key | meta_value |
+---------+---------+-----------------------+------------+
| 29570 | 9465 | rz_is_claimed | 1 |
| 29571 | 9465 | rz_location__lat | 42.9875625 |
| 29572 | 9465 | rz_location__lng | 13.8987698 |
| 29573 | 9465 | rz_location__geo_city | Palma |
| 29577 | 9291 | rz_is_claimed | 1 |
| 29578 | 9291 | rz_location__lat | 11.8976543 |
| 29579 | 9291 | rz_location__lng | 14.9087654 |
| 2958 | 9291 | rz_location__geo_city | Axminster |
+---------+---------+-----------------------+------------+
The best I can come up with is:
$posts = $wpdb->get_results(
$wpdb->prepare("
SELECT p.post_title, p.ID, p.post_type, p.post_status, m.post_id, m.meta_value, m.meta_key
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} m
ON ( p.ID = m.post_id )
WHERE p.post_type = %s
AND p.post_status = %s
AND ( ( p.post_title LIKE '%%$wordone%%' OR p.post_title LIKE '%%$wordtwo%%' ) OR ( m.meta_key = 'rz_location__geo_city' AND ( m.meta_value LIKE '%%$wordone%%' OR m.meta_value LIKE '%%$wordtwo%%' ) ) )
",
$post_type,
$post_status
)
);
The problem with this request is that if I type the word "the", I return an array of results for the same post, but I'm expecting one result. This is also true if I search for "the community axminster", "the stackoverflow". Funnily enough, if I search for "axminster anna" then I get one result for 'The Community Waffle House' plus an array of results for Vila Anna, but in actual fact I should be returning zero results. And if I search for "Palma Axminster" I get two results but I should be returning zero.
Any help much appreciated. I'm not sure if it's necessary to say this but in the query above, for simplicity, I wrote p.post_title LIKE '%%$wordone%%' OR p.post_title LIKE '%%$wordtwo%%'
however in reality I'm taking the search term and doing the following:
$words = explode(' ', $search_term);
$conds = array();
foreach ($words as $val) {
$conds[] = "p.post_title LIKE '%%".$val."%%'";
}
$implode_statement = implode(' OR ', $conds);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论