2013-05-30 25 views
0

安裝自定義查詢到WP_Query,WordPress的

select 
    tab1.post_id, 
    ((tab1.meta_value - tab2.meta_value)/tab1.meta_value) * 100 as discount 
from 
    fridaysunday.wp_postmeta as tab1, 
    fridaysunday.wp_postmeta as tab2 
where 
    tab1.post_id = tab2.post_id and tab1.meta_key = 'price' and tab2.meta_key = 'sale_price' 
order by discount desc; 

我想把這個查詢與WP_Query功能集成以下查詢。 非常感謝您的幫助,提前致謝!

回答

1
<?php 

global $wpdb; 
$prefix = $wpdb->prefix; 

$query = 'select 
    '.$prefix.'tab1.post_id, 
    (('.$prefix.'tab1.meta_value - '.$prefix.'tab2.meta_value)/'.$prefix.'tab1.meta_value) * 100 as discount 
from 
    '.$prefix.'wp_postmeta as tab1, 
    '.$prefix.'wp_postmeta as tab2 
where 
    '.$prefix.'tab1.post_id = '.$prefix.'tab2.post_id and '.$prefix.'tab1.meta_key = 'price' and '.$prefix.'tab2.meta_key = 'sale_price' 
order by discount desc"; 

$records = $wpdb->get_results($query); 



?> 
+0

你怎麼整合這個查詢與WP_Query()? – Rao