Hot answers tagged

2

Fixed query select p.ID as order_id, p.post_date, i.order_item_name, max( CASE WHEN im.meta_key = '_product_id' and i.order_item_id = im.order_item_id THEN im.meta_value END ) as Prod_ID from wp_posts as p, wp_postmeta as pm, wp_woocommerce_order_items as i, wp_woocommerce_order_itemmeta ...


2

cracks knuckles Alright -- so first thing is first. Working w/ WordPress Databases Read this: https://codex.wordpress.org/Class_Reference/wpdb Avoid write SQL statements outside of the $wpdb object. Avoid writing SQL statements like wp_users instead do this: $users = $wpdb->get_results( "SELECT FROM $wpdb->users" ); This will help if you ever ...


2

SQL-wise, you only need to join to the wp_posts table once. Joining to the terms stuff will give you multiple rows, so it's probably easiest to group these and then use GROUP_CONCAT() to flatten the terms into a comma-separated string (updated to use LEFT joins): global $wpdb; $sql = $wpdb->prepare( 'SELECT p.ID, p.post_title AS Product, ...



Only top voted, non community-wiki answers of a minimum length are eligible