我正在使用wordpress。网站上有很多产品。页面中有一个价格排序过滤器(从低到高,从高到低)。
问题是我的产品价格不是数字而只是文本(即P.O.R.)。在两种情况下,我都要求在结果结束时使用这些产品。
我的代码需要使用sql查询而不是get_posts()等...所以我构建了2个查询:在第一个查询中我得到的产品不是文本,在第二个查询中我是获取带有文本的产品(在第二个查询中执行,以便它们将在最后)。这是我的代码:
$sql1 = "SELECT * FROM wp_posts AS table1 INNER JOIN (SELECT * FROM wp_postmeta WHERE
meta_key = 'product_price' AND meta_value != 'P.O.R.' order by (meta_value + 0) ASC) AS
table2 ON table1.ID = table2.post_id WHERE table1.ID IN ($currentActiveProductIds) AND
table1.post_type = 'product-items' AND table1.post_status = 'publish'";
$sql2 = "SELECT * FROM wp_posts AS table1 INNER JOIN (SELECT * FROM wp_postmeta WHERE
meta_key = 'product_price' AND meta_value = 'P.O.R.') AS table2 ON
table1.ID = table2.post_id WHERE table1.ID IN ($currentActiveProductIds)
AND table1.post_type = 'product-items' AND table1.post_status = 'publish'";
$sql = $sql1." UNION ".$sql2;
问题是订购工作不正常。这是因为第二次查询,因为当我只留下第一个查询时,它在两种情况下都被正确排序。
答案 0 :(得分:0)
使用连接时,无法在内部查询中使用order by .. 我做了一些改变,它应该工作..
$sql1 = "SELECT * FROM wp_posts AS table1 INNER JOIN (SELECT * FROM wp_postmeta WHERE
meta_key = 'product_price' AND meta_value != 'P.O.R.') AS
table2 ON table1.ID = table2.post_id WHERE table1.ID IN ($currentActiveProductIds) AND
table1.post_type = 'product-items' AND table1.post_status = 'publish'";
$sql2 = "SELECT * FROM wp_posts AS table1 INNER JOIN (SELECT * FROM wp_postmeta WHERE
meta_key = 'product_price' AND meta_value = 'P.O.R.') AS table2 ON
table1.ID = table2.post_id WHERE table1.ID IN ($currentActiveProductIds)
AND table1.post_type = 'product-items' AND table1.post_status = 'publish'";
$sql = $sql1." UNION ".$sql2;