Hello
Have you got in your csv file products IDs?
I mean next, if you have csv file with data like:
id,slug
123,new-slug-for-product-123
456,new-slug-for-product-456
You can apply next function:
function update_product_slugs_from_csv() {
$csv_file_path = ABSPATH . 'path/to/your/file.csv';
$file_handle = fopen($csv_file_path, 'r');
if ($file_handle !== FALSE) {
global $wpdb;
fgetcsv($file_handle);
while (($data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$product_id = intval($data[0]);
$new_slug = sanitize_title($data[1]);
$slug_exists = $wpdb->get_var($wpdb->prepare("SELECT post_name FROM {$wpdb->posts} WHERE post_name = %s AND ID != %d", $new_slug, $product_id));
if (!$slug_exists) {
$wpdb->update(
$wpdb->posts,
['post_name' => $new_slug],
['ID' => $product_id]
);
echo "Slug for product ID $product_id updated to $new_slug.\n";
} else {
echo "Slug $new_slug for product ID $product_id is not unique and was not updated.\n";
}
}
fclose($file_handle);
} else {
echo "Failed to open the CSV file.\n";
}
}
If you know wjat is wp-cli do next:
* to functions.php add next code:
if (defined('WP_CLI') && WP_CLI) {
WP_CLI::add_command('update_product_slugs', 'update_product_slugs_from_csv');
}
* You can now run this feature using the following WP-CLI command: wp update_product_slugs
-
This reply was modified 8 months, 2 weeks ago by RealMag777.