Basically when the meta_key field is equal to 'location' I want to add an extra where clause to make a comparison based on that location.
I've tried this with both Case and IF statements but can't seem to get it to work, any ideas where I'm going wrong?
$query = "
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type,
wp_postmeta.meta_key, wp_postmeta.meta_value, wp_postmeta.post_id,
wp_term_relationships.object_id, wp_term_relationships.term_taxonomy_id,
wp_term_taxonomy.term_id,
wp_terms.term_id, wp_terms.name
FROM wp_posts
/* Join Post Meta Table */
JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
/* Join Term Relationships Table to get the taxonomy id */
JOIN wp_term_relationships
ON wp_posts.ID = wp_term_relationships.object_id
/* Join Term Taxonomy Table to get the Term ID */
JOIN wp_term_taxonomy
ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
/* Finally Join to the Terms table to get the Category Name *sigh* */
JOIN wp_terms
ON wp_term_taxonomy.term_id = wp_terms.term_id
WHERE post_type = 'listing' AND
wp_postmeta.meta_key IN ('locations', 'email', 'phone-number')";
if(isset($location)) {
$query .= "
IF wp_postmeta.meta_key='location'
THEN AND wp_postmeta.meta_value LIKE '".mysql_real_escape_string($location)."'
END IF";
}
Thanks and let me know if I haven't explained anything clearly enough!
if(isset($location)) {
$query .= " AND
CASE
WHEN wp_postmeta.meta_key='location' THEN wp_postmeta.meta_key
LIKE '".mysql_real_escape_string ($location)."'
ELSE 1=1
END ";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With