将自定义查询附加到WP_Query,wordpress

时间:2013-05-30 10:20:54

标签: wordpress wordpress-plugin wordpress-theming

我有以下查询

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 个答案:

答案 0 :(得分: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);



?>