Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

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?

share|improve this answer
    
Thanks Evan, I will make sure to create meta keys for this in future if I ever need to order a taxonomy term. –  Joshc Mar 1 '13 at 9:54
add comment

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);
share|improve this answer
add comment

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'
));
share|improve this answer
    
... so long as the meta_value actually is a number. There are more than a couple of posts here where people are trying to numerically sort things that aren't numbers :) @heathenJesus, can you provide sample code. Link only answers are discouraged. –  s_ha_dum Feb 20 '13 at 15:44
    
The question specifically stated "custom taxonomy with a numeric term value." Added a demonstration of using 'meta_value_num'. –  heathenJesus Feb 21 '13 at 21:03
1  
Oh, I know. That was not in any way critical of your answer. I just don't, in general, trust people when they say "numeric". Sometimes it means something like "$100" or "123-04". Those are colloquially numbers but are not numeric to the software. And I work with an office full of people like that so I've learned to specify very carefully. –  s_ha_dum Feb 21 '13 at 21:18
    
Fair enough, @s_ha_dum –  heathenJesus Feb 21 '13 at 21:22
    
@heathenJesus so you saying I can actually query taxonomy terms the same as querying custom fields? I thought mete keys and registered taxonomy terms were two separate things... For example could I query using your method a category term via 'meta_key' ? I didn't think you could do that...? –  Joshc Feb 23 '13 at 0:32
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.