wordpress wpdb クラスを使用して、メタ キーの 2 つの値のいずれかでカスタム投稿を返し、それらの投稿を 3 番目のメタ値で並べ替えようとしています。必要な投稿を取得できますが、希望どおりに並べ替える方法がわかりません。
私がこれまで持っている機能は次のとおりです。
function artists_search_country($country_alt){
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND ((wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = '$country_alt')
OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = '$country_alt'))
AND wposts.post_type = 'artists'
AND wposts.post_status = 'publish'
";
$myposts = $wpdb->get_results($querystr, OBJECT);
return $myposts;
}
私は次のようなことをしたいと思います:
function artists_search_country($country_alt){
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND ((wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = '$country_alt')
OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = '$country_alt'))
AND wposts.post_type = 'artists'
AND wposts.post_status = 'publish'
ORDER BY (wpostmeta.meta_key = '_artist_artist_last_name' AND wpostmeta.value) ASC
";
$myposts = $wpdb->get_results($querystr, OBJECT);
return $myposts;
}
行の構文が不十分であることをお許しください。ORDER BY
ただし、これにアプローチする方法がわかりません。私が試みるすべてがクエリを壊します。SQLクエリの経験があまりないので、どんな助けでも大歓迎です。