function bulk_update_product_image_alt_text() {
// Only run once — remove after execution
if ( ! isset( $_GET['run_alt_fix'] ) ) return;
// ⬇️ Replace 'your_post_type' with your actual custom post type slug
$posts = get_posts([
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
]);
$updated = 0;
$skipped = 0;
foreach ( $posts as $post ) {
// --- Featured Image ---
$thumbnail_id = get_post_thumbnail_id( $post->ID );
if ( $thumbnail_id ) {
$current_alt = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
if ( empty( $current_alt ) ) {
update_post_meta(
$thumbnail_id,
'_wp_attachment_image_alt',
'Wheel Hub Bearing ' . $post->post_title // Uses the post title as alt text
);
$updated++;
} else {
$skipped++;
}
}
}
echo "Done! Updated: {$updated} | Skipped (already had alt text): {$skipped}";
exit;
}
add_action( 'init', 'bulk_update_product_image_alt_text' );