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 am learning WP_query and recently ran into a problem.

I need to do a query where i first filter trough one or more custom fields, which works great thanks to meta_query.

However, i also need it to order by multiple custom fields.

(Like in sql you can write "ORDER BY field ASC, field2 DESC")

This is what i have so far:

` $args=array( 'post_type' => 'personer',

                    'meta_query' => array(
                        array(
                            'key' => 'p_seller',
                            'value' => 'Y',
                            'compare' => '='
                        )
                    ),
                    'meta_key' => 'p_region',
                    'orderby' => 'meta_value',
                    'order' => 'ASC',
                    'post_status' => 'publish',
                    'posts_per_page' => 999,
                    'caller_get_posts'=> 1
                );`

It returns rows with p_seller set to Y and order by p_region. I'd also like to add a second orderby which order them by p_lastname after region. Or even better - p_region, p_lastname, p_firstname. All ascending, but i'd love to know how to combine ASC and DESC aswell :)

I have done some googling, but most people seem to write about multiple fields for filtering, which i got. Or they do space out in custom SQL queries, which i hope i can get by without? :)

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I am sure there are more elegant solutions, but this is what i came up with and it works for now...

global $wpdb;

                $querystr = "
                    SELECT wposts.*
                    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2, $wpdb->postmeta wpostmeta3
                    WHERE wposts.ID = wpostmeta.post_id
                    AND wposts.ID = wpostmeta2.post_id
                    AND wposts.ID = wpostmeta3.post_id
                    AND wpostmeta.meta_key = 'p_seller'
                    AND wpostmeta2.meta_key = 'p_role'
                    AND wpostmeta3.meta_key = 'p_firstname'
                    AND wpostmeta.meta_value = 'N'
                    AND wposts.post_type = 'personer'
                    AND wposts.post_status = 'publish'
                    ORDER BY wpostmeta2.meta_value ASC, wpostmeta3.meta_value ASC
                ";

                $pageposts = $wpdb->get_results($querystr);

                if ($pageposts){
                    foreach ($pageposts as $post){
                        setup_postdata($post);

                                                     // the_title() or whatever works here..              

                    }
                }
share|improve this answer
    
I agree plain SQL does the trick. I would use aliases for comprehension. –  sglessard Feb 19 at 3:35
add comment

You should customize the query via posts_orderby filter

In your template

add_filter('posts_orderby', 'custom_orderby');
$query= new WP_Query($args);
remove_filter('posts_orderby', 'custom_orderby');

In your functions.php

function custom_orderby($orderby) {
    return "ORDER BY field ASC, field2 DESC";
}
share|improve this answer
    
Hmm, did not work.. seems kind of weird that it should since that seems like targeting regular sql fields but i guess custom fields are not even in the posts table in the database? –  jonas Oct 31 '12 at 15:29
    
no custom fields are not in the same table, they are in the wp_postmeta table. There are filters available for customizing the full query(near line 2600 in wp-includes/query.php). Use posts_clauses to var_dump the query that is being used. It just needs a little tweaking. –  Mridul Aggarwal Oct 31 '12 at 19:04
    
Sorry but if anyone could give me a working example to reverse engineer it would help a lot! –  jonas Nov 6 '12 at 8:14
    
Here is working example: pluginus.net/… –  realmag777 Feb 13 '13 at 8:53
add 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.