Skip to content

Instantly share code, notes, and snippets.

@andykillen
Created September 5, 2023 14:34
Show Gist options
  • Save andykillen/1288e6f2fd81b1f611b4351c51215cbb to your computer and use it in GitHub Desktop.
Save andykillen/1288e6f2fd81b1f611b4351c51215cbb to your computer and use it in GitHub Desktop.
Faster updates
<?php
global $wpdb;
$query = $wpdb->prepare("SELECT post_id, table1.meta_value as sku, table2.meta_value as price ".
"FROM {$wpdb->postmeta} as table1 ".
"JOIN {$wpdb->postmeta} as table2 USING (post_id) ".
"WHERE ".
"table1.meta_key = '_sku' AND ".
"table2.meta_key = '_price' ".
"GROUP BY post_id".
";");
$results = $wpdb->get_results($query);
$file = file_get_contents("feed.json");
$json_array = json_decode($file, true);
$working_array = [];
foreach ( $json_array as $product_data ) {
$working_array[$product_data['sku']] = [
'price' => $product_data['rrp']
];
}
foreach($results as $product) {
if(isset($working_array[$product->sku]) && $working_array[$product->sku]['price'] != $product->price) {
update_post_meta( $product->post_id, '_price', $working_array[$product->sku]['price'], $product->price );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment