
In a recent Business Bloomer Club Slack thread, a user sought help with bulk updating products using WP CLI after importing over 500 products via WP All Import. While the import process was successful, an issue persisted: the product breadcrumbs were still showing the old product category.
Interestingly, the problem was resolved simply by resaving the product, even without making any changes. The user was looking for a more efficient way to trigger this update for all products without manually saving each one.
WP CLI is a powerful tool for WordPress users, providing a command-line interface for managing WordPress sites. It can be used to perform bulk updates and automate various tasks, including product updates for WooCommerce. However, WP CLI doesn’t natively support product imports or updates with WP All Import, and the specific issue of re-saving products to refresh breadcrumbs required some creative thinking.
Understanding the Problem with Product Breadcrumbs
The issue at hand was that after importing products via WP All Import, the breadcrumbs still showed the old product category. The problem was not with the import itself but with the way WordPress handled product data after the import was completed. The solution, as discovered by the user, was to “fake” a save of the product. This action triggered a refresh of the product’s data, which ultimately corrected the breadcrumb display.
This situation typically occurs when product categories or taxonomy terms are updated during the import but aren’t reflected in the frontend due to caching or WordPress not reprocessing the product’s metadata. While this can often be resolved by manually saving each product, doing this for hundreds or thousands of products is not practical.
Bulk Updating Products with WP CLI
WP CLI is often used to manage WordPress sites through the command line. While WP CLI doesn’t directly support WP All Import, it can be used to update product data in bulk. For this use case, the goal is to trigger a “fake” save for all products, which can be done by updating each product’s post meta.
Using WP CLI to Trigger a Product Update
Here’s how you can trigger a save for all products using WP CLI:
- Identify the Product IDs: You need to know the IDs of the products you want to update. These can be retrieved using WP CLI commands like
wp post list
with the appropriate post type (e.g.,product
).wp post list --post_type=product --format=ids
This command will return a list of product IDs. - Use WP CLI to Update Each Product: Once you have the list of product IDs, you can use a WP CLI loop to trigger the update for each product. The
wp post update
command allows you to update a post’s meta data or trigger a save:wp post update <product_id> --post_title="Updated Product Title" --post_status=publish
You can loop through all the product IDs and trigger the update for each one. The product title or other meta values can be left unchanged, as the goal is just to refresh the product data and trigger the breadcrumbs to update. - Automating the Process: If you want to automate this process for multiple products, you can write a custom script that iterates over the product IDs and applies the update. Here’s a basic example:
#!/bin/bash for product_id in $(wp post list --post_type=product --format=ids); do wp post update $product_id --post_status=publish done
This script will update each product’s status and trigger the necessary actions, including the breadcrumb refresh.
Considerations and Downsides
While WP CLI provides a powerful way to bulk update products, there are some downsides compared to using WP All Import or other import plugins:
- No Image Support: WP CLI does not handle image uploads. If you need to update product images, WP All Import or another plugin will be more suitable.
- Meta Data Updates: If your products rely on custom meta fields (e.g., from Advanced Custom Fields), you need to ensure that you’re updating the correct meta keys. WP CLI doesn’t automatically detect custom fields, so you’ll need to manually specify them in your script.
- Plugin-Specific Fields: For products with fields added by other plugins, you’ll need to be aware of their meta keys and ensure they are included in the update process.
Conclusion
Using WP CLI to bulk update products is a viable solution for WooCommerce users who need to perform mass updates without going through the manual process of saving each product. While WP CLI doesn’t offer native support for all product updates, such as image handling or custom fields, it can still be used to trigger updates for general product data. For users looking to automate this process, writing a custom script to update products is a powerful approach that can save time and effort.
If you’re facing issues with product data not updating correctly after imports or need to refresh specific elements like breadcrumbs, WP CLI offers a reliable method to address these problems efficiently. However, keep in mind the limitations and plan accordingly based on your site’s specific needs.