5

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?

Thanks!

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.

1
  • What is the SQL query you are using? Oct 10, 2012 at 19:25

3 Answers 3

2

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?

1
  • 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! Oct 11, 2012 at 7:16
1

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:

SELECT 
 p.ID, p.post_content,
 p.post_title,
 p.post_excerpt, 
 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'
0

for Yoast meta data:

SELECT 
 p.ID,
 p.post_title,
 p.post_name as url,
 pm1.meta_value as description,
 pm2.meta_value as meta_title
FROM wp_posts p
LEFT JOIN wp_postmeta pm1
 ON p.ID = pm1.post_id
 AND pm1.meta_key = '_yoast_wpseo_metadesc'
LEFT JOIN wp_postmeta pm2
 ON p.ID = pm2.post_id
 AND pm2.meta_key = '_yoast_wpseo_title'
WHERE post_type = 'page' AND post_status = 'publish'
/* WHERE post_type = 'post' AND post_status = 'publish' */
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 5, 2021 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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