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


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.



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

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')

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


add_action('pre_get_posts', 'modify_query');
| improve this answer | |

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.