I have a bit complex wp query which is based on custom fields. It's probably more related to SQL it self...
query:
$querystr = "SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->term_taxonomy.term_id = $cat->cat_ID
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND (
$wpdb->posts.post_end_date IS NULL
OR
$wpdb->posts.post_end_date >= DATE_FORMAT(NOW(), '%Y-%m-%d')
)
AND $wpdb->postmeta.meta_key = 'Type'
AND $wpdb->postmeta.meta_value != 'Offer'
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC
I need to fetch entries that are newer then todays date (this works) and based on the one additional custom field "Type". Field can have values "code" or "offer" or it's not defined.
Now... this query works for all entries that have custom field "type" defined, but the problem is it doesnt work on the ones that dont have custom field "type" set.
So I need to test if the field is null OR is code/offer and fetch them all together. Any help would be much appreciated.
Thanks!