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.

1) Custom Post Type named "Buying_Locations"

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

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' 
    ); 
share|improve this question
add comment

2 Answers

Haven't checked this, but a quick look at the Codex says that your query should just include 'Orderby' => 'state city name'

share|improve this answer
    
I edited the code example above. Got a little further along with it. Maybe you have some insight on how to add the extra 'meta_key'? Please see above. Thanks much. –  Mark Rapp Feb 11 '13 at 23:13
1  
Apologies - it was late and my brain wasn't compeltely in gear so I failed to notice the fact that I was typing 3 meta_key's! I've done a little more searching and I think you're beyond the capabilities of the wordpress core. I think the answer is in filtering the OrderBy element: add_filter('posts_orderby', 'edit_posts_orderby');, however my SQL is pretty poor! Sorry I couldn't help more, I would be interested if you get a solution though –  Jamie Wright Feb 12 '13 at 10:06
    
I found this (wordpress.mcdspot.com/2012/10/24/…) and I'm right there with adjusting it to suit my needs. The last thing to figure out is how to get the final 3rd sorting on the Post's Title. –  Mark Rapp Feb 12 '13 at 15:32
add comment

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.

share|improve this answer
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.