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 a taxonomy wp_query and i would like to order the list by title and by meta value (numeric value)

  • Have a meta value Interesting = 1 or 0 in the posts
  • Not so interesting posts would be at the bottom of the query

GOAL - OUTPUT LIKE THIS: (is this possible with WP_QUERY and WP3.1)

A ( META KEY interesting = 1)

B ( META KEY interesting = 1)

C ( META KEY interesting = 1)

A ( META KEY interesting = 0)

B ( META KEY interesting = 0)

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You can filter the orderby part of the query to get what you want (trying to pass it via the orderby parameter will not work, it will be filtered out). This simple example adds the meta_value sort order before the standard title sort order.

add_filter( 'posts_orderby', 'wpse15168_posts_orderby' );
$query = new WP_Query( array(
    'meta_key' => 'interesting',
    'orderby' => 'title',
    'order' => 'ASC',
) );
remove_filter( 'posts_orderby', 'wpse15168_posts_orderby' );

function wpse15168_posts_orderby( $orderby )
{
    global $wpdb;
    $orderby = $wpdb->postmeta . '.meta_value DESC, ' . $orderby;
    return $orderby;
}
share|improve this answer
1  
thanks for the code! Is it possible somehow to move add_filter to functions.php instead of having it in a template? I'm constructing $args array and passing it to new WP_Query($args). In the functions.php I've added the filter and trying to test for my CPT as get_query_var('post_type') == 'custom-post-type-name'. But this fails. The reason I want to add the filter code to functions.php is because the orderby is the global thing throughout the website for my CPT and used in quite a few templates. Hope that makes sense. Thank you! –  dashaluna May 2 '11 at 17:23
1  
@DashaLuna: Moving it to functions.php should be no problem. If you use a separate WP_Query` you should probably not use get_query_var() since that will read the global $wp_query object, not your current custom query. Use the second parameter that is passed to the posts_orderby filter instead. –  Jan Fabry May 3 '11 at 6:20
    
thank you very much! Yep, that was my mistake as the function was 'looking' at the global $wp_query, which was displaying a page. I've modifed the function that the posts_orderby filter calles to include 2nd query param as following: properties_list_orderby( $orderby, $pqry ) and then I can test if my CPT is queried as following: if( $pqry->query_vars['post_type'] == 'custom-post-type-name'). Thanks a lot again :) –  dashaluna May 3 '11 at 16:49
    
Is there a way to do this and trigger the ordering by URL? –  Jonathan Oct 17 '11 at 18:54

you can't mix ascending and descending order, but if you switch the zeroes and ones then this might work:

$query = new WP_Query( array( 'meta_key => 'interesting',
                              'orderby' => 'meta_value_num title',
                              'order' => 'ASC' ) );
share|improve this answer

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.