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'
);
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:
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…
And then later on, ends with…
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.
Haven’t checked this, but a quick look at the Codex says that your query should just include
'Orderby' => 'state city name'