mysql查询以列出所有wocommerce产品

时间:2019-02-04 07:57:02

标签: mysql wordpress woocommerce

使用现有的wordpress woocommerce网站开发一个电子商务codeigniter应用程序,我不确定我是否使用正确的方法,但是无论如何,在codeigniter中,我都会初始化第二个数据库连接到我的wordpress数据库。 这是我正在codeigniter中执行的mysql查询,它列出了我正在寻找的产品,但没有显示产品价格。

$sql = "SELECT p.ID,p.post_title,pm1.meta_value,`post_content`,`post_excerpt`,t.name AS product_category,t.term_id AS product_id,t.slug AS product_slug,tt.term_taxonomy_id AS tt_term_taxonomia,tr.term_taxonomy_id AS tr_term_taxonomia,
        MAX(CASE WHEN pm1.meta_key = '_price' THEN pm1.meta_value ELSE NULL END) as price,
        MAX(CASE WHEN pm1.meta_key = '_regular_price' then pm1.meta_value ELSE NULL END) as regular_price,
        MAX(CASE WHEN pm1.meta_key = '_sale_price' then pm1.meta_value ELSE NULL END) as sale_price,
        MAX(CASE WHEN pm1.meta_key = '_sku' then pm1.meta_value ELSE NULL END) as sku,
        ( SELECT guid FROM wp_posts WHERE id = pm1.meta_value ) AS imgurl
        FROM wp_posts p 
        LEFT JOIN wp_postmeta pm1 ON pm1.post_id = p.ID AND pm1.meta_key =  '_thumbnail_id'
        LEFT JOIN wp_term_relationships AS tr ON tr.object_id = p.ID
        JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id 
        JOIN wp_terms AS t ON t.term_id = tt.term_id
        WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' AND t.slug = 'uncategorized'
        GROUP BY p.ID,p.post_title ORDER BY pm1.meta_value+0 DESC";

0 个答案:

没有答案