Skip to content

Instantly share code, notes, and snippets.

@levantoan
Created April 26, 2016 15:22
Show Gist options
  • Save levantoan/a385147073ebcdd8574a3fbf2e16f32c to your computer and use it in GitHub Desktop.
Save levantoan/a385147073ebcdd8574a3fbf2e16f32c to your computer and use it in GitHub Desktop.
Truy vấn 1 lúc nhiều điều kiện với meta_key và meta_value trong WordPress
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id )
INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id )
WHERE 1=1
AND (
wp_postmeta.meta_key = 'end_date'
AND
(
( mt1.meta_key = 'tick_event' AND CAST(mt1.meta_value AS CHAR) = '1' )
AND
( mt2.meta_key = 'start_date' AND CAST(mt2.meta_value AS DATE) >= '2016-4-21' )
AND
( mt3.meta_key = 'end_date' AND CAST(mt3.meta_value AS DATE) >= '2016-4-21' )
)
)
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value
DESC LIMIT 0, 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment