Created
June 23, 2015 09:48
-
-
Save robertuniqid/f82c78169afac160efd5 to your computer and use it in GitHub Desktop.
WooCommerce - Get Newest Order ID By UserID and Product ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
$query = 'SELECT oinfo.ID | |
FROM ' . $wpdb->base_prefix . 'woocommerce_order_items as oitem | |
LEFT JOIN ' . $wpdb->base_prefix . 'posts as oinfo ON oinfo.ID = oitem.order_id | |
LEFT JOIN ' . $wpdb->base_prefix . 'postmeta as ometa ON ometa.post_id = oinfo.ID AND ometa.meta_key = "_customer_user" | |
LEFT JOIN ' . $wpdb->base_prefix . 'woocommerce_order_itemmeta as oimeta ON oimeta.order_item_id = oitem.order_item_id | |
AND oimeta.meta_key = "_product_id" | |
AND oimeta.meta_value = ' . $product_id . ' | |
WHERE ometa.meta_value=' . $user_id . ' ORDER BY oinfo.ID DESC'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure how I haven't seen your reply until now :) great optimization, when I've coded this it was mostly for internal use, so I didn't go for speed optimizations.
I personally love left joins because they still work if the database is damaged for some reason, and the results are null.