I’m writing an SQL query for WordPress that searches for events that are live between a users search. I’m hard-coding it at the moment to test.

At the moment, two events are being returned for every time the post_id appears in the wp_postmeta table, can anyone see where there may be an issue with this logic?

SELECT ID, post_title 
    FROM $wpdb->posts AS post 
    INNER JOIN $wpdb->postmeta AS meta
    ON (post.ID = meta.post_id)
    INNER JOIN $wpdb->postmeta AS meta2
    ON (post.ID = meta2.post_id)
    WHERE post.post_status = 'publish'
    AND post.post_type = 'event' 
    AND post.post_author = '1' 
    AND (
           ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value < '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value > '20140630') )
        OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value < '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value >= '20140620') )
        OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value > '20140620') AND (meta.meta_key = 'date_0_start-date' AND meta.meta_value <= '20140630') )
        OR ( (meta.meta_key = 'date_0_start-date' AND meta.meta_value >= '20140620') AND (meta2.meta_key = 'date_0_end-date' AND meta2.meta_value <= '20140630') )
    )

Read more here: SQL returns duplicate results


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , ,

Wordpress related questions and answers: