I was trying to teach myself something new and having a look at the way WordPress structures it’s tables to work with revisions.
The structure is (sorta) as follows:
+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title | post_date | post_name | post_content | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
| 1 | Foo | 2012-09-20 10:00:00 | Foo | Bar | 0 |
| 2 | Arrrrr | 2012-09-20 10:05:00 | Arrrr | Pirates! | 0 |
| 3 | Arrrrrr | 2012-09-20 10:06:00 | revision-1 | Argh pirates | 2 |
| 4 | Arrrrrr | 2012-09-20 10:06:00 | revision-2 | Argh piratessss | 2 |
+----+------------+---------------------+------------+-----------------+-------------+
Now, I’d like to make a query that gives me ONLY the parent rows (Foo and Arrrr) with their ID’s, BUT with their revised content.
I came up with following query:
SELECT original.ID, revision.post_title, revision.post_content, revision.post_name FROM wp_posts AS original
INNER JOIN wp_posts AS revision ON original.ID = revision.post_parent
WHERE original.post_status = 'publish'
AND original.post_parent = 0
ORDER BY original.ID, revision.ID DESC
This gives me following result:
+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title | post_date | post_name | post_content | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
| 1 | Foo | 2012-09-20 10:00:00 | Foo | Bar | 0 |
| 2 | Arrrrrr | 2012-09-20 10:06:00 | revision-2 | Argh piratessss | 2 |
| 2 | Arrrrrr | 2012-09-20 10:06:00 | revision-1 | Argh pirates | 2 |
| 2 | Arrrrr | 2012-09-20 10:05:00 | Arrrr | Pirates! | 0 |
+----+------------+---------------------+------------+-----------------+-------------+
But I’d like to further reduce it to:
+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title | post_date | post_name | post_content | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
| 1 | Foo | 2012-09-20 10:00:00 | Foo | Bar | 0 |
| 2 | Arrrrrr | 2012-09-20 10:06:00 | revision-2 | Argh piratessss | 2 |
+----+------------+---------------------+------------+-----------------+-------------+
I’ve tried adding DISTINCT to the SELECT, and adding GROUP BY original.ID but both didn’t give me the desired result.
While you can’t distinguish different revisions of one topic for each other except date you can do the following: