我是Wordpress编程的新手,我想制作自定义代码,问题在于:
我从几乎相同的SQL查询中获得了不同的值。第一个来自$wpdb->get_results()
函数,另一个来自add_filter()
函数。
那我为什么要这样做呢?它是因为我有一个数据库,其中的项目有不同的货币。因此,我希望在将其更改为默认货币后对价格进行排序,在我的情况下为USD
。 (我已经有了这个功能),所以问题是即使它已经相似,我也不会得到相同的post ID
以下是我在$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
");
结果是:
ID: 40168
ID: 39832
ID: 40346
ID: 40846
ID: 42200
ID: 39687
ID: 39467
ID: 29605
ID: 32620
ID: 32773
ID: 35175
ID: 17
它们之间有很多代码
这个来自add_filter
函数:
$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 . ")";
以下是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
ID顺序已经正确但查询显示的另一篇文章不是ID
的确切$wpdb
值,那为什么会发生?
还是有更好的方法可以代替这个吗?我感谢任何帮助,谢谢