Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Last active May 28, 2018 03:09
Show Gist options
  • Save seanbehan/7313492 to your computer and use it in GitHub Desktop.
Save seanbehan/7313492 to your computer and use it in GitHub Desktop.
Export your wordpress db (post comments, tags and categories) as JSON
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