$wpdb – sort by two meta value

I have “type” custom field and two value (free and paid). I need get “paid” posts first in loop, and free latest. How can I sort my query? I can’t use 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;
  ";


Source: wpdb

Leave a Reply