Accessing WordPress from Ruby: Part II
Part of the ruby-wpdb series
- Accessing WordPress from Ruby: Part I
- Accessing WordPress from Ruby: Part II
- Accessing WordPress from Ruby: Part III
Background
In part one, I discussed how you can use ruby-wpdb to access a WordPress database from within Ruby. In this article, part two, I’ll take a look at some “recipes” - quick snippets that achieve some common tasks, and hopefully give you an idea of what ruby-wpdb is capable of.
Working with posts
Within WordPress, the single most important object is probably the post.
WordPress encourages you to segment types of content by creating custom
post types; these are just rows in the posts table like any other post,
with differing post_type
values.
It’s probably fair to say, then, that the most common tasks people want to perform with WordPress involve posts. Here are some ideas for what can be done with ruby-wpdb.
Displaying the five most recent posts
Deleting old drafts
Fetching posts according to postmeta
e.g. to fetch all posts that have postmeta with the key “foo” and the value “bar”:
Processing comments
When doing batch operations with WordPress I often find myself working with comments. Whether it’s clearing the moderation queue or processing comments as input to another process, you can do it with ruby-wpdb.
Listing the most recent unmoderated comments
Deleting unmoderated comments over a year old
Reading options
If you’d like your scripts to be flexible across installs and to behave similarly to WordPress itself, sooner or later you’re going to want to check an option.
ruby-wpdb offers the full interface to options that you’d expect. But if you’d just like to check an option’s value, it offers a shortcut function:
Advanced filtering
Under the hood, ruby-wpdb uses Sequel to connect to the WordPress database. This gives you enormous power and flexibility: anything you can do with Sequel, you can do with WordPress.
Particularly useful reading is the documentation for dataset filtering; this demonstrates the power of Sequel at performing complex queries without you having to drop down into writing SQL.
That’s it! Hopefully you can see some common threads across these examples that you can translate to your own tasks.
In part three, I look at ruby-wpdb’s support for Gravity Forms, a popular plugin used for capturing user input — showing you how ruby-wpdb can make analysing and exporting GravityForms data a breeze.
Add a comment