I have this query…
<?php
$press = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'individual',
'post_status' => 'private'
));
if ($press->have_posts()) : while ($press->have_posts()) : $press->the_post();
?>
But my custom post-types are using custom taxonomy with a numeric term value.
My question is there anyway in ordering this query by the term value?
The taxonomy is called ‘individual-group’
Any help would be hugely appreciated thanks.
Josh
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
No, there is no way to do this with default WP Core. @heathenJesus talks about meta data not taxonomies. See http://scribu.net/wordpress/sortable-taxonomy-columns.html for a proper solution.
And a more thorough explanation of why this is not something built into Core: Using wp_query is it possible to orderby taxonomy?
Method 2
Here is the complex query I used to numerically sort and filter the posts query by taxonomy slugs.
$my_post_type = 'some_post_type';
$my_term_slug_to_be_used_for_Ordering = 'some_slug';
$my_term_slug_to_be_used_for_Filtering = 'some_other_slug';
$querystr = "
SELECT
$wpdb->posts.*, $wpdb->term_relationships.object_id,
(SELECT wpt.slug
FROM $wpdb->term_taxonomy wptt
INNER JOIN $wpdb->term_relationships wptr ON wptt.term_taxonomy_id = wptr.term_taxonomy_id
INNER JOIN $wpdb->terms wpt ON wpt.term_id = wptt.term_id
WHERE
wptr.object_id = $wpdb->term_relationships.object_id AND
wptt.taxonomy = 'my_term_slug_to_be_used_for_Ordering') AS numeric_column_value
FROM $wpdb->term_taxonomy INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
INNER JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id
WHERE
$wpdb->terms.slug = '{$my_term_slug_to_be_used_for_Filtering}' AND
$wpdb->posts.post_type = '$my_post_type' AND $wpdb->posts.post_status = 'publish'
ORDER BY numeric_column_value * 1 DESC"; // Actually it is a string so, * 1 for numeric
$r = $wpdb->get_results($querystr, OBJECT);
Method 3
Sure, in the orderby parameter, there is an option called meta_value_num which should do exactly what you are looking for.
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
$press = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'individual',
'post_status' => 'private',
'orderby' => 'meta_value_num',
'meta_key' => 'individual-group'
));
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0