Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a theme released into the wild but have not prefixed the post type appropriately. Yes, I know, it's a shame and I regret it :(

So, I am currently using acme as the post type name, but I' like to change it to prefix_acme.

I'm looking for the most appropriate function/hook to rename the post type without losing any data so users will not experience a difference.

Can the database values under wp_posts -> post_type simply be changed during a theme update?

If so, then can the same be done for wp_postmeta -> meta_key without losing the meta_value?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

This will not be the full answer but i think it will greatly help you if you are able to make templates - you will be able to do this:

In theme init (when it installs) make function which will make db query to find all post with your post_type and renames post_type to your new value. (i think this is very easy and can be done with 1 query).

Then make sure your new theme supports that new post_type (just change post_type name) - THAT's IT! CLients won't loose anything and you'll fix it in seconds :)

MAybe this query will do it: (not tested)

function fixme($oldname, $new_typename){
global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE ".$wpdb->posts." SET post_type = %s WHERE post_type = %s", $new_typename, $oldname ) );                                                                                                                                                        

}

You can use in function this: $wpdb->prefix if you need to get prefix.

OR you can use add_action( 'after_setup_theme', 'your_function_name' ); so after theme will be installed it will call your defined function, but then you have to change it to not use parameters to set new post_type but write them manually (assign to $oldname, $new_typename values)

And YES you can do the same on meta keys too (if they are not sanitized, if they are -then you'll need php more than in regular situation)..you can do whatever you want with queries, cause they are direct to database.

share|improve this answer
    
Hey Tom, yes this is really helpful! I will try your suggestions and report back. –  fjarrett May 10 '12 at 13:07
    
Worked like a charm! :) Here's my code, I am actually looping through a specified array of post types and adding a prefix: gist.github.com/2653299 –  fjarrett May 10 '12 at 14:20
    
Glad that this helped :) Also if you select my answer as correct one - people would know that this helps solve the problem you had with no need to read all comments :) And of course this will also adds for me some points which is like reputation, and having good rep is nice :D –  Tommixoft May 10 '12 at 15:51
    
Of course :) Done. –  fjarrett May 10 '12 at 19:35
    
Also updated the gist to be more robust, checking first to see if the new post type already exists :) –  fjarrett May 10 '12 at 23:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.