0

I have a page that lists out the members of our band, as a custom post type.

I am trying to order the members by several custom fields, field by category, or "section, then by their class, and then by name. All these are entered as custom fields.

Any idea on how to do this? Here is the page: http://www.uconnband.org/members

0

You can do it with WP_Query:-

$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value', 'meta_key' => 'price' ) );

Replace 'price' with your meta key.

Reference:-

http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  • But what if I want to order by all 3 fields? – user3699892 Aug 2 '14 at 21:05
0

you can do something like this

//here is the code:-

function modify_query($query){
 //set this input in the orderby form
//and check if its from the form and the request something like this
if(!isset($_GET['check']) || $_GET['check'] != 'check')
    return;

//then you can change the query as per request
if ($query->is_main_query() && isset($_GET['orderby'])) {
    $query->set('orderby', 'meta_value' );
    switch($_GET['orderby']){
        case 'class':
            $query->set('meta_key', 'some_key');
            break;
        case 'section':
            $query->set('meta_key', 'other_key');
            break;
        //add like this
    }

}
}

add_action('pre_get_posts', 'modify_query');

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.