long story short: I need to retrieve data from WordPress-created custom Table and add/update metadata to the related post (post_type=product). I can match the posts (products) and custom table fields by post metadata (_sku) which is the key of my query.
To give you more context here follows my query:
SELECT * FROM wpfx_custom_table a
left join wpfx_postmeta b
on a.custom_key_id=b.meta_value
WHERE b.meta_key = '_sku'
ORDER BY a.custom_date DESC
LIMIT 1
I have tried this but it does not work:
function update_custom_post_meta_data() {
global $wpdb;
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'products',
'suppress_filters' => true,
'meta_key' => '_sku'
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array) {
$post_array->the_post();
$post_id = $post_array->ID;
$post_sku = get_post_meta($post_id,'_sku',true);
$CustomTable = $wpdb->prefix.'custom_table';
$results = $wpdb->get_results ( "SELECT * FROM CustomTable WHERE `custom_key_id` = $meme_sku ORDER BY `custom_date` DESC LIMIT 1");
foreach($results as $result){
$custom_meta_value = $result->custom_column1;
add_post_meta($post_id, 'custom_meta_key', $custom_meta_value);
}
update_post_meta($post_id, 'custom_meta_key', $custom_meta_value);
}
}
Thank you in advance