Skip to content

Instantly share code, notes, and snippets.

@jondcampbell
Created May 26, 2015 23:03
Show Gist options
  • Save jondcampbell/353e70c419882890308d to your computer and use it in GitHub Desktop.
Save jondcampbell/353e70c419882890308d to your computer and use it in GitHub Desktop.
Find all woocommerce orders that have a particular product_id in it. Returns an array of order ids.
global $wpdb;
$sql = "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = $product_id ) AND order_item_type = 'line_item'";
$order_ids = $wpdb->get_col( $sql );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment