Last active
March 10, 2017 10:21
-
-
Save vovadocent/992fc0ebe92d2ba4751e76fecf3c8497 to your computer and use it in GitHub Desktop.
WP query order by taxonomy name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getTermOrderedIds($post_type, $taxonomy) { | |
global $wpdb; | |
$sql = "SELECT p.ID FROM $wpdb->posts p | |
JOIN $wpdb->term_relationships tr ON (tr.object_id = p.ID) | |
JOIN $wpdb->term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id AND tt.taxonomy = '$taxonomy') | |
JOIN $wpdb->terms t ON (t.term_id = tt.term_id) | |
WHERE p.post_status = 'publish' AND post_type = '$post_type' ORDER BY t.name ASC"; | |
$res = $wpdb->get_results($sql); | |
if ($res) { | |
$ids = array_map(function($e) { | |
return $e->ID; | |
}, $res); | |
return $ids; | |
} | |
return false; | |
} | |
$ids = getTermOrderedIds('posttype_name', 'taxonomy_name'); | |
$args = array( | |
'post_type' => 'posttype_name', | |
'post_status' => 'publish', | |
'orderby' => 'post__in', | |
'post__in' => $ids | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment