Sort Posts by Multiple Meta Values

1) Custom Post Type named “Buying_Locations”

2) Each Post has three meta values of: State, City, Store Name

Read More

QUESTION:
How can you show a list of these posts sorted alphabetically by State, then City and then the Store Name? For example…

STATE 1

  • CITY 1

    • STORE NAME
    • STORE NAME
    • STORE NAME
  • CITY 2

    • STORE NAME
    • STORE NAME
    • STORE NAME
  • CITY 3

    • STORE NAME
    • STORE NAME
    • STORE NAME

STATE 2

  • CITY 1

    • STORE NAME
    • STORE NAME
    • STORE NAME
  • CITY 2

    • STORE NAME
    • STORE NAME
    • STORE NAME

STATE 3

  • CITY 1

    • STORE NAME
    • STORE NAME
  • CITY 2

    • STORE NAME
    • STORE NAME
    • STORE NAME
  • CITY 3

    • STORE NAME

I’ve gotten a little further with this:

$mypost = array( 
'post_type' => 'buy_locations', 
'posts_per_page' => -1, 
'meta_key' => 'store_city', 
'orderby' => 'meta_value title', 
'order' => 'ASC' 
); 
$loop = new WP_Query( $mypost ); 

Just need to figure out how to sort by the State first, then the city, then title.

Not sure how to add the extra “meta_key” value.

How do I add the 3rd sorting request?

$mypost = array( 
    'post_type' => 'buy_locations', 
    'posts_per_page' => -1, 
    'meta_key1' => 'store_state', 
    'meta_key2' => 'store_city', 
    'orderby' => 'meta_value1 meta_value2 title', 
    'order' => 'ASC' 
    ); 

Related posts

Leave a Reply

2 comments

  1. Since I couldn’t find the way to grab the custom post’s title and add it to the sorting/ordering by meta_key, I edited my custom post type to include a hidden field that grabs the title of the post and sets it as a custom meta value when the user adds a new custom post. So, in my custom post type plugin, I have:

    <input type="hidden" name="buy_location_name" value="<?php echo get_the_title($buy_location->ID); ?>" />
    
    if ( isset( $_POST['buy_location_name'] ) && $_POST['buy_location_name'] != '' ) 
     { update_post_meta( $buy_location_id, 'location_name', get_the_title($buy_location_id) ); }
    

    And then on the frontend page that posts the results sorted by State, City and finally Store name, I use the example code found in this URL: http://wordpress.mcdspot.com/2012/10/24/sort-posts-on-multiple-custom-fields/

    My code starts like so…

    $meta_keys = array('store_state','store_city','location_name');
    $args = array(
        'post_type' => 'buy_locations',
        'meta_key' => $meta_keys[0],
        'orderby' => 'meta_value',
        'posts_per_page' => -1);
    $my_query = new WP_Query($args);
    

    And then later on, ends with…

    usort($my_query->posts, 'sort_3_keys');
    while ( $my_query->have_posts() ) : $my_query->the_post();
    $id = $post->ID;
    $storestate = $id_array[$id][$meta_keys[0]];
    $storecity = $id_array[$id][$meta_keys[1]];
    $storename = $id_array[$id][$meta_keys[2]];
    echo "$storestate | $storecity | $storename <br />";
    endwhile;
    

    In summary, the cat who developed this code (http://wordpress.mcdspot.com/2012/10/24/sort-posts-on-multiple-custom-fields/), is one bad muthah and I’m grateful for his work and for him sharing it. This has helped me achieve the results I needed. Cheers.