Currently I am working with a project where I have to install Woo Commerce but previous developer create a custom post type product. We need to update this to some other name so we don’t get any conflict in future.
Current name: products need to update to old_products
Step 1: Update the Post Type
Use the following SQL query to update the post type from “product” to “old_product”:
UPDATE wp_posts SET post_type = 'old_product' WHERE post_type = 'product';
Alternatively, you can use the wp_update_post function with the wp_insert_post hook:
function update_post_type() {
$args = array(
'post_type' => 'old_product',
);
wp_update_post($args);
}
add_action( 'wp_insert_post', 'update_post_type' );
Step 2: Update the Taxonomy
Use the following SQL query to update the taxonomy name from “product-categories” to “old-product-categories”:
UPDATE wp_term_taxonomy SET taxonomy = 'old-product-categories' WHERE taxonomy = 'product-categories';
Alternatively, you can use the wp_update_term function with the wp_insert_term hook:
function update_taxonomy() {
$term_id = 123; // Replace with the term ID
$taxonomy = 'old-product-categories';
wp_update_term($term_id, 'product-categories', array(
'taxonomy' => $taxonomy,
));
}
add_action( 'wp_insert_term', 'update_taxonomy' );
Step 3: Update the ACF Field Group
- Go to the ACF settings page.
- Find the field group associated with the “product” post type.
- Update the post type to “old_product”.
- Save the changes.
Step 4: Update the ACF Fields
Use the following code to update the ACF fields
function update_acf_fields() {
$field_name = 'field_123'; // Replace with the field name
$new_value = 'New value'; // Replace with the new value
acf_update_field($field_name, $new_value, 'old_product');
}
add_action( 'acf/update_field', 'update_acf_fields' );
Step 5: Verify the Changes
- Go to the WordPress admin dashboard.
- Verify that the post type and taxonomy have been updated correctly.
- Check that the ACF fields are also updated correctly.
By following these steps, you should be able to update your custom post type, taxonomy, and ACF fields using queries and hooks.