Why does $wpdb->get_result() return value differen

2019-08-01 16:37发布

I'm new to Wordpress programming, I want to make a custom code, the problem here is:

I got the different values from almost identical sql queries. The first one from $wpdb->get_results() function and the another one from the add_filter() function.

So why I would do this? it's because I have a database, where the items have different currency. So I would like to sort the price after change it to the default currency which is USD in my case. (I already have the function tho), so the problem is I don't get the same post ID even though it's already similar

Here is the query I wrote on $wpdb

$results = $wpdb->get_results("
    SELECT 
        wp_posts.ID AS id,
        price.meta_value AS price_value,
        currency.meta_value AS currency_value
    FROM
        wp_posts 
        INNER JOIN " . $wpdb->postmeta . " ON (" . $wpdb->posts . ".ID = wp_postmeta.post_id) 
        INNER JOIN " . $wpdb->postmeta . " AS currency ON (" . $wpdb->posts . ".ID = currency.post_id AND currency.meta_key = 'month_currency') 
        INNER JOIN " . $wpdb->postmeta . " AS price ON (" . $wpdb->posts . ".ID = price.post_id AND price.meta_key = 'month_price') 
    WHERE 
        1=1 
        AND ((wp_postmeta.meta_key = 'monthly' AND wp_postmeta.meta_value = 'Y')) 
        AND wp_posts.post_type = 'post' 
        AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
    GROUP BY 
        wp_posts.ID 
    LIMIT 
        0, 12
");

And the result is:

ID: 40168
ID: 39832
ID: 40346
ID: 40846
ID: 42200
ID: 39687
ID: 39467
ID: 29605
ID: 32620
ID: 32773
ID: 35175
ID: 17

There are a lot of code here between them

And this one is from the add_filter function:

$mam_global_fields = ', price.meta_value';
$mam_global_join   = " 
    INNER JOIN " . $wpdb->postmeta . " AS currency ON (" . $wpdb->posts . ".ID = currency.post_id AND cur.meta_key = 'month_currency')
    INNER JOIN " . $wpdb->postmeta . " AS price ON (" . $wpdb->posts . ".ID = price.post_id AND price.meta_key = 'month_price')";
$mam_global_orderby = "FIELD(" . $wpdb->posts . ".ID" . $order_id . ")";

And here is the query that created by the add_filter()

SELECT 
    SQL_CALC_FOUND_ROWS DISTINCT wp_posts.*, 
    price.meta_value 
FROM 
    wp_posts 
    INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
    INNER JOIN wp_postmeta AS currency ON (wp_posts.ID = currency.post_id AND currency.meta_key = 'month_currency') 
    INNER JOIN wp_postmeta AS price ON (wp_posts.ID = price.post_id AND price.meta_key = 'month_price') 
WHERE 
    1=1 
    AND ((wp_postmeta.meta_key = 'monthly' AND wp_postmeta.meta_value = 'Y')) 
    AND wp_posts.post_type = 'post' 
    AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 
GROUP BY 
    wp_posts.ID 
ORDER BY 
    FIELD(wp_posts.ID, 40168, 39832, 40346, 40846, 42200, 39687, 39467, 29605, 32620, 32773, 35175, 17)
LIMIT 
    0, 12

The ID order already correct but the query showing another post which is not the exact ID value like the $wpdb, so why it's happened?

Or is there any better way that I can use instead this one? I appreciate any kind of help, thank you

0条回答
登录 后发表回答