1

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)

1

2 Answers 2

5

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;
}
4
  • 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, 2011 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, 2011 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, 2011 at 16:49
  • Is there a way to do this and trigger the ordering by URL?
    – Jonathan
    Oct 17, 2011 at 18:54
1

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' ) );
2
  • You actually can mix ascending/descending... 'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) (per... codex.wordpress.org/Class_Reference/WP_Query) Feb 5, 2015 at 21:06
  • 1
    Yes @Stephanie you can do that since version 4.0. Back in 2011 when I wrote my answer that was not possible. Thank you for the update!
    – windyjonas
    Feb 6, 2015 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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