Post Type Switcher and WPML fix

The Post Type Switcher plugin by John James Jacoby is a really handy way to convert an existing post to a new or different post type. Imagine, for example, that you’ve been maintaining a site for a while, using classic WordPress posts and categories to publish information about your products. But the day has come when your products really need to be defined separately from other editorial content you publish on your site. Once you’ve created your new Products post type (either by using a plugin or hiring a professional like myself), Post Type Switcher lets you move those existing articles from Posts to Products with the click of a button. Pretty sweet.

Now imagine that your website exists in more than one language. Hopefully, you are using WPML to manage that multi-lingual content, as in my mind it’s the best plugin out there right now. As of this writing, Post Type Switcher does not handle the conversion of your posts for more than one language, and unless you manually go into your database and update the appropriate tables, your content kinda gets lost in the mix. It will actually disappear from your admin view. Not good.

Thanks to this WordPress.org forum post by turegjorup, there is a solution.

Disable the plugin if it’s already active, then, from the plugin’s folder, open the file post-type-switcher.php, and just after the line set_post_type( $post_id, $new_post_type_object->name ); add the following code:

if(function_exists('icl_object_id')){
// adjust field 'element_type' in table 'wp_icl_translations'
// from 'post_OLDNAME' to 'post_NEWNAME'
// the post_id you look for is in column: 'element_id'

     if($post->post_type == 'revision'){
	if(is_array($post->ancestors)){
	     $ID = $post->ancestors[0];
	}
     } else {
	$ID = $post->ID;
	}
     global $wpdb;

     $wpdb->update(
          $wpdb->prefix.'icl_translations',
	  array('element_type' => 'post_' . $new_post_type_object->name,	), // string
	  array('element_id' => $ID,'element_type' => 'post_' . $post->post_type)
	  );

     $wpdb->print_error();
}

Reactivate the plugin, and you’re good to go. I also tested the Convert Post Types plugin by Stephanie Leary, which presents the same conflict. I’m sure a similar fix could be created for it as well.

Jenny Beaumont

Jenny Beaumont is a multicultural, multidisciplinary maker and writer of things. She works as a Sr. Project Manager at Human Made, speaks at conferences in France and abroad, contributes to a number of blogs, and is a former organizer of WordCamp Paris and WordCamp Europe.

Follow me on Twitter | Subscribe to my newsletter

8 thoughts on “Post Type Switcher and WPML fix

  1. I’m having the same problem at the moment but the fix above (which seems to be in the 1.2 version of post type switcher) doesn’t quite work. It moves the English version but not the translation. I’m switching from one CPT to another.

    Maybe I’m being stupid but do I need to paste that code in exactly as is? (There is an error in it, where it says // string, you’ve commented out the bracket as well).

    • Hi Rob,

      Thanks for pointing out the error in the code, I’ve gone ahead and updated it. Sorry you’re having trouble with this too, this is the fix that worked for me. I did just copy this code into the plugin’s main file after the line I indicated above. It allowed not only the posts to switch post types (the main problem), but also the translations in WPML to follow. I don’t know if the plugin has been updated since I wrote this…will have to check on that. Again, this code goes in the plugin file, not your theme file…hope that helps.

      best,
      -jennyb

  2. From what I can tell, this fix no longer works with the most recent version of the plugin.

    The plugin author updated the code a couple of weeks ago and this line no longer exists:

    set_post_type( $post_id, $new_post_type_object->name );

Comments are closed.