使用SQL查询以CSV格式导出WordPress帖子和元信息

时间:2012-10-12 09:15:39

标签: mysql sql wordpress csv

道歉,这是一个duplicate of entry of this post,但我发现我没有得到正确的答案而且不确定如何要求将其移至SO?

我需要使用CSV格式的SQL查询导出所有WordPress帖子和相关的元信息。我设法只导出帖子,但意识到元信息在另一个表格中。

有人可以告诉我,我该怎么做呢?

谢谢!

编辑:我已经尝试了很多插件,但没有一个适用于这个特定场景。我确实需要处理查询本身。感谢

编辑:CSV文件的最终结果应如下所示。在一行中,这些是我最终会得到的列。 (我将删除CSV中的其他列,我只是在这里提供缩短版本)

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

每行一个帖子,同一行中的元信息。

编辑:有了S-ha-dum,我已经成功了,但我有大约12个meta_value> meta_key值要退出数据库,所以我需要帮助来编写其余的查询

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 个答案:

答案 0 :(得分: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

感谢大家的意见!

答案 1 :(得分:0)

尝试使用多个CSV导出插件。我使用this one效果很好。

答案 2 :(得分:0)

我认为您需要的所有内容都在* _posts和* _postmeta表中。

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

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

您需要枚举字段,而不是使用asterix或课程。对于每个帖子,您将需要循环并组织多个行。

其次,编写子查询。

"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}"

在这种情况下,子查询应该可以正常运行。再次,枚举你的字段。

答案 3 :(得分: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

<强>结果:

Custom Fields Inline Export Result