Skip to content

Instantly share code, notes, and snippets.

@koen12344
Created January 30, 2024 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koen12344/52f11d249ee6fd8ac26723bf52d5e8f5 to your computer and use it in GitHub Desktop.
Save koen12344/52f11d249ee6fd8ac26723bf52d5e8f5 to your computer and use it in GitHub Desktop.
Clear references to uploaded images in the Product Sync for GBP
<?php
/*
* Clears Product Sync for GPB image references on Google and forces re-upload
*
* Copy snippet into the Code Snippets plugin (https://wordpress.org/plugins/code-snippets/) and set it to only run once, then execute it
*
*/
function psfg_clear_all_image_references(){
$args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
];
$post_ids = get_posts($args);
foreach($post_ids as $post_id){
$created_posts = get_post_meta($post_id, '_psfg_created_products', true);
if(!empty($created_posts) && is_array($created_posts)){
foreach($created_posts as $created_post){
unset( $created_post->image);
}
}
update_post_meta($post_id, '_psfg_created_products', $created_posts);
}
}
add_action('init', 'psfg_clear_all_image_references');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment