'Wordpress query post_title and meta_value for search term
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|