Rename a current used post type

Hi guys I’m working on this public theme where the previous developer had created a post type called “blog” the problem is that when you create a page with the same slug it creates a conflict.

The theme is being used by several clients and I’m trying to avoid this issue by renaming the post type without having the users/clients to run by any problem.

I’m doing the following:

$theme_executed = false;
if(!$theme_executed) {
 theme_blog_news();
 $theme_executed = true;
}

function theme_blog_news(){
  $theme_sql= mysql_query("UPDATE  wp_posts SET  post_type="news" WHERE  post_type="blog"");
  if (!$theme_sql) {
    die('Invalid query: ' . mysql_error());
  }
}

It worked but I’m looking for a best practice or a more native way.

Thanks

2 Answers
2

You are in the right direction. When doing something like this, make sure you look at every possible place where this needs to be changed as you don’t want data inconsistency. Some suggestions to make this process better:

Don’t hardcode WordPress table name ($wpdb->posts) in the query. This won’t work if the install has a custom table prefix set in their wp-config.php

Save the version no of ur plugin and write upgrade functions which works on the basis of versions and then you can call them individually or in a batch checking how many upgrade functions needs to be run. Everytime you change something like this make a function that you will call if a client is upgrading from X version and then you can chain upgrade functions in a batch if the client hasn’t updated from a long time.

Leave a Comment