I'm trying to query two Wordpress tables, ie. posts
and postmeta
.
Basically the tables and rows I'm interested looks like this:
| ---- ** posts ** ---- | | -------- ** postmeta ** ---------- |
| ID | post_type | | post_id | meta_key | meta_value |
|-----------------------| |------------------------------------|
| 1 | player | | 1 | number | 10 |
| 2 | player | | 2 | number | 20 |
| 3 | player | | 3 | othre_key | aaa |
| 4 | other_type | | 4 | other_key | xxx |
| 4 | other_type | | 5 | other_key | yyy |
| 4 | other_type | | 6 | other_key | zzz |
I want to get all posts where post_type = player
and order them by postmeta.meta_value
where the postmeta.meta_key = number
whether or not the postmeta
row exists.
so far I've got this:
SELECT a.ID, a.post_title, b.meta_value, b.meta_key
FROM $wpdb->posts AS a
INNER JOIN $wpdb->postmeta AS b
ON a.ID = b.post_id
AND b.meta_key = %s
WHERE a.post_status = %s
AND a.post_type = %s
GROUP BY a.ID
ORDER BY b.meta_value+(0) ASC, a.post_title ASC
...which returns the players that has a number postmeta associated with them.
How do I append the players that doesn't have that postmeta to the result within same query so that the desired result would look something like this?
| --- ** result ** ---- |
| ID | meta_value |
|-----------------------|
| 1 | 10 |
| 2 | 20 |
| 3 | null |