I need to export all WordPress Posts and related Meta information using an SQL query. I've managed to export just the Posts but realised that the Meta information is in another table.

Could someone please tell me how I would go about doing this please?


EDIT: The final outcome of the CSV file should look something like this.

[POSTS TABLE] post_content | post_title | post_excerpt | Author (meta_key) | [META TABLE] AuthorName (meta_value) | Publisher (meta_key) | PublisherName (meta_value) | BookCondition (meta_key) | BookCondition (meta_value)

So one post per row with the meta information in one row but in the same column.

  • What is the SQL query you are using? – markratledge Oct 10 '12 at 19:25

What is the SQL query you are using? What are more details of the export and end format you need?

The obvious things are 1) http://wordpress.org/extend/plugins/navayan-csv-export/

"Navayan CSV Export is a wordpress plugin to export all your table data in CSV (Comma Separate Value) format."

and 2), exporting with phpmyadmin - the database utility - into CSV. http://www.phpmyadmin.net/home_page/index.php

phpmyadmin is usually available on web hosts. Use "Export" and then select CSV.

phpmyadmin is also available as a plugin, though only the older versions of the plugin seem bug free: http://wordpress.org/extend/plugins/portable-phpmyadmin/

Will either of these work?

  • Hi Songdogtech. I am currently just using the regular export feature built into phpMyAdmin and exporting the CSV file. What I need though, is an SQL query that will pull all the posts and its related Meta information. I could go the plugin route, but this client is going to need a lot more work done to the DB than any other client I have worked with in the past, I so I need to work on my SQL queries going forward. Many thanks for all the suggestions, I will keep them in my WP Toolbag! – SixfootJames Oct 11 '12 at 7:16

I needed to do exactly the same and found a way. With multiple joins you will get all meta_values in one row per post ID:

 p.ID, p.post_content,
 pm1.meta_value as author,
 pm2.meta_value as publisher,
 pm3.meta_value as book_condition
FROM wp_posts p
LEFT JOIN wp_postmeta pm1
 ON p.ID = pm1.post_id
 AND pm1.meta_key = 'author'
LEFT JOIN wp_postmeta pm2
 ON p.ID = pm2.post_id
 AND pm2.meta_key = 'publisher'
LEFT JOIN wp_postmeta pm3
 ON p.ID = pm3.post_id
 AND pm3.meta_key = 'book_condition'
WHERE post_type = 'books' AND post_status = 'publish'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.