Flutter to Custom Post Types

So I’m working a project right now where I’m converting a WordPress installation from Flutter to the native WordPress custom post type functionality. The reasons for the switch are numerous. For one, it’s always good rule of thumb to use as few plugins as possible. The fewer plugins, the fewer php warning messages, the fewer javascript conflicts, the fewer hooks and filters getting processed on every load, and the fewer hits on the Database server. Secondly, flutter is particularly hard on the database, sometimes adding an extra 5 or more queries for every custom field.

The really great thing about flutter is that it still stores the field values in the  wp_postmeta table which means you don’t have to worry about any real data migration. So you have the luxury of taking it a step at a time.

The first step is to convert those key categories attached to “write panels” into post types. I did this by registering the post type as usual and simply running the following function on the init hook. This function performs a simple DB query to update posts with the specified cat slug into post_types.


function convert_cats_to_types($cat,$type) {
global $wpdb;
$query = $wpdb->prepare("
UPDATE wp_posts SET post_type = '$type' WHERE ID IN
(
SELECT DISTINCT object_id FROM wp_term_relationships rel
INNER JOIN wp_term_taxonomy AS tt ON tt.term_taxonomy_id = rel.term_taxonomy_id AND tt.taxonomy = 'category'
INNER JOIN wp_terms AS t ON t.term_id = tt.term_id AND t.slug = '$cat'
)
");
$results = $wpdb->query($query);
}

There’s no risk of losing data here, we are just changing where the posts in question are show up. The second thing to do is to replace the field-specific metaboxes. We’ll cover that in the next post.