Last active
May 28, 2018 03:09
-
-
Save seanbehan/7313492 to your computer and use it in GitHub Desktop.
Export your wordpress db (post comments, tags and categories) as JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require 'wpdb' | |
require 'json' | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'mysql', | |
:host => 'localhost', | |
:port => 3306, | |
:username => 'root', | |
:password => '', | |
:database => 'your_db_name' | |
) | |
post_data = [].tap do |post_data| | |
Wpdb::WpPost.posts.published.each do |post| | |
data = {} | |
data[:title] = post.post_title | |
data[:slug] = post.post_name | |
data[:body] = post.post_content | |
data[:tags] = post.wp_tags | |
data[:categories] = post.wp_categories | |
data[:published_at] = post.post_date | |
post.wp_comments.each do |comment| | |
(data[:comments]||=[]) << comment.attributes | |
end | |
post_data << data | |
end | |
end | |
puts post_data.to_json | |
# or even XML | |
puts post_data.to_xml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment