3

Apologies, this is a duplicate of entry of this post but I found I'm not getting the right answer there and not sure how to ask to move it over to SO?

I need to export all WordPress Posts and related Meta information using an SQL query in CSV format. 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: I have already tried a number of plugins, none of which have worked for this specific scenario. I really do need to work on the query itself. Thanks

EDIT: The final outcome of the CSV file should look something like this. On one row, these are the columns that I will end up with. (I will delete the additional columns in CSV, I'm just giving a shortened version here)

post_content | post_title | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value* | **meta_key** | *meta_value* 

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

EDIT: With the S-ha-dum, I've managed to get some way, but I have about 12 meta_value > meta_key values to pull out of the DB so I need help to write the rest of the query please

SELECT *,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Publisher' AND wp_postmeta.post_id = wp_posts.ID) as Publisher FROM wp_posts
4

4 回答 4

5

最后,一位朋友设法帮助我解决了我的问题。这是他将所有带有元信息的帖子导出到 CSV 文件的最后一个 SQL 查询。

SELECT DISTINCT
post_title
, post_content
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) as "Asking Price (US\$)"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (ZAR)' AND wp_postmeta.post_id = wp_posts.ID) as "Asking Price (ZAR)"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Author' AND wp_postmeta.post_id = wp_posts.ID) as Author
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Binding' AND wp_postmeta.post_id = wp_posts.ID) as Binding
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Condition' AND wp_postmeta.post_id = wp_posts.ID) as "Book Condition" 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Number' AND wp_postmeta.post_id = wp_posts.ID) as "Book Number" 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Book Type' AND wp_postmeta.post_id = wp_posts.ID) as "Book Type"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Edition' AND wp_postmeta.post_id = wp_posts.ID) as  Edition
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Illustrator' AND wp_postmeta.post_id = wp_posts.ID) as Illustrator 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Inscription' AND wp_postmeta.post_id = wp_posts.ID) as Inscription 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'ISBN' AND wp_postmeta.post_id = wp_posts.ID) as  ISBN
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Jacket Condition' AND wp_postmeta.post_id = wp_posts.ID) as "Jacket Condition"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Location' AND wp_postmeta.post_id = wp_posts.ID) as  Location
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Published Place' AND wp_postmeta.post_id = wp_posts.ID) as "Published Place"
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Published Year' AND wp_postmeta.post_id = wp_posts.ID) as "Published Year" 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Publisher' AND wp_postmeta.post_id = wp_posts.ID) as Publisher 
,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Size' AND wp_postmeta.post_id = wp_posts.ID) as Size
FROM wp_posts
WHERE post_type = 'post' 
ORDER BY
post_title
, post_content

谢谢各位的意见!

于 2012-10-14T17:00:48.297 回答
0

自定义字段内联 SQL 导出查询

我想出了一个看起来很激烈的查询。它使用列中的自定义字段提取帖子,内联。请注意,查询中的“Concat”是在一些文本中欺骗相关帖子,您可以自行决定添加更多自定义字段。我的 SQL 应用程序(Sequel Pro)有一个 CSV 导出功能,我在查询远程数据库后使用它。工作非常好。

SELECT *

FROM (

    SELECT 
        `wp_posts`.`ID` , 
        `wp_posts`.`post_title` AS `Title`,
        `wp_posts`.`post_date` AS `Date`,

        MAX( CASE WHEN `wp_postmeta`.`meta_key` = 'useremail'
        THEN `wp_postmeta`.`meta_value`
        END ) AS `Email`,

        MAX( CASE WHEN `wp_postmeta`.`meta_key` = 'usercell'
        THEN `wp_postmeta`.`meta_value`
        END ) AS `Cell Phone`,

        concat('http://yoururl.com/?p=',`wp_posts`.`ID`) as `URL`

    FROM `wp_posts`

    LEFT JOIN `wp_postmeta` ON ( `wp_posts`.`ID` = `wp_postmeta`.`post_id` )

    WHERE `wp_posts`.`post_status` = 'publish'

    AND `wp_posts`.`post_type` = 'customposttype'

    GROUP BY `wp_posts`.`ID`

    ORDER BY `wp_posts`.`post_date` DESC

) AS `t` WHERE 1 =1

结果:

自定义字段内联导出结果

于 2013-02-26T07:56:29.020 回答
0

尝试许多 CSV 导出插件。我用这个效果很好。

于 2012-10-12T09:22:54.140 回答
0

我认为您需要的一切都在 *_posts 和 *_postmeta 表中。

有两种方法可以做到。首先,只需在 *_post 表上加入 *_postmeta 表。

"SELECT * FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id"

您将希望枚举您的字段,而不是使用 asterix 或 course。您将不得不循环和组织每个帖子的多行。

其次,编写子查询。

"SELECT *,(SELECT meta_value FROM {$wpdb->postmeta} WHERE {$wpdb->postmeta}.meta_key = 'Publisher' AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID) as Publisher,... FROM {$wpdb->posts}"

子查询在这种情况下应该表现得很好。再次枚举您的字段。

于 2012-10-12T14:40:11.313 回答