我有“输入”自定义字段和两个值(免费和付费)。我需要先循环获得“付费”帖子,并免费获得最新信息。如何对查询进行排序?我不能使用wp_query。
global $wpdb;
$query = "
SELECT *
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta m1
ON ( $wpdb->posts.ID = m1.post_id )
WHERE
$wpdb->posts.post_type = 'object'
AND $wpdb->posts.post_status = 'publish'
AND ( m1.meta_key = 'type' AND m1.meta_value = $type )
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date
DESC;
";
答案 0 :(得分:0)
您目前正在按post_date排序,因此最旧的将首先显示,最后一个显示最新的。对于同一日期的所有行,mysql将决定订单。如果您想保留此日期序列,但始终首先在特定日期显示付费日期,那么您只需添加第二个排序属性:
ORDER BY $wpdb->posts.post_date, meta_value DESC
DESC将确保“付费”在“免费”之前出现,如果这些是您的实际价值。这也假设只有两个值。如果你可能有更多的值,那么调查mysql CASE expression
是值得的答案 1 :(得分:0)
很简单。
global $wpdb;
$query = "
SELECT *
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta m1
ON ( $wpdb->posts.ID = m1.post_id )
INNER JOIN $wpdb->postmeta m2 //add this line
ON ( $wpdb->posts.ID = m2.post_id ) //add this line
WHERE
$wpdb->posts.post_type = 'object'
AND $wpdb->posts.post_status = 'publish'
AND ( m1.meta_key = 'city' AND m1.meta_value = $city_string )
AND ( m2.meta_key = 'type' ) //add this line
GROUP BY $wpdb->posts.ID
ORDER BY m2.meta_value //add this line
DESC;
";