Created
August 8, 2015 14:48
-
-
Save fomigo/bde3e1c3122211882fed to your computer and use it in GitHub Desktop.
wpdb queries with meta data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$results = $wpdb->get_results( | |
" | |
SELECT key2.meta_value as cat_id, $wpdb->posts.ID as action_id -- , $wpdb->posts.post_title as title | |
FROM $wpdb->posts | |
INNER JOIN $wpdb->postmeta key1 | |
ON $wpdb->posts.ID = key1.post_id | |
INNER JOIN $wpdb->postmeta key2 | |
ON $wpdb->posts.ID = key2.post_id | |
WHERE key1.meta_key = 'active' | |
AND key1.meta_value = '1' | |
AND key2.meta_key = 'term4action' | |
AND key2.meta_value != 0 | |
ORDER BY $wpdb->posts.post_date | |
" | |
); | |
$actions = array(); | |
foreach ($results as $result) { | |
$actions[] = $result; | |
} | |
dump($actions); | |
$results = $wpdb->get_results( | |
" | |
SELECT key2.meta_value as cat_id, $wpdb->posts.ID as action_id -- , $wpdb->posts.post_title as title | |
FROM $wpdb->posts | |
INNER JOIN $wpdb->postmeta key1 | |
ON $wpdb->posts.ID = key1.post_id | |
INNER JOIN $wpdb->postmeta key2 | |
ON $wpdb->posts.ID = key2.post_id | |
WHERE key1.meta_key = 'active' | |
AND key1.meta_value = '1' | |
AND key2.meta_key = 'term4action' | |
AND key2.meta_value = 14 | |
ORDER BY $wpdb->posts.post_date DESC | |
" | |
); | |
$actions = array(); | |
foreach ($results as $result) { | |
$actions[] = $result; | |
} | |
dump($actions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment