The idea: To pull a list of trails in a specific state, or a city in a state.
The database table is wp_postmeta, the columns in the table that we will be using are post_id, meta_key, and meta_value.
post_id can be the same in multiple rows
meta_key must either be “location_state” “location_city” or “trail_type”
So go through each row in the database, look for post_meta.meta_value = 'Arizona' and grab the post_id from that same row
THEN foreach post_id it found in that state look for a value in post_meta.meta_key = 'trail_type' with a matching post_id. This means that the post_id is associated with a trail, and not something else. Return an array of the post_id’s which are trails in that state.
to an array called $trails_in_state
Now we have gotten a list of all trails in a state by ID.
I would also like to get all trails in a city of that state into a separate query, so that I can say when the page is hit
<?php if( isset($_GET['city']) AND isset($_GET['state']) ) {
// GET AN ARRAY OF ALL post_id's OF TRAILS IN THAT CITY STATE COMBO
} elseif( isset($_GET['state'])) {
// GET AN ARRAY OF ALL post_id's OF TRAILS IN THAT STATE
}
Here is an image of the table structure http://imgur.com/ijhR1NZ
I’m using wordpress so can’t easily use mysqli in this case.
I think these should get you started. The first should select you all your post_id’s where you’re meta_value is equal to an input. I’m not sure why you need to limit by state or city a broad match should work just fine. A more specific match to filter on city only would be the 2nd option.
SELECT trail_type FROM wp_postmeta WHERE post_id=(SELECT post_id FROM wp_postmeta WHERE meta_value=’Arizona’)
and
SELECT trail_type FROM wp_postmeta WHERE post_id=(SELECT post_id FROM wp_postmeta WHERE meta_value=’Arizona’ AND meta_key=’location_city’ )
Failing that you’re either looking at multi selects and sorting or perhaps a UNION query I just can’t get my head around that table structure seems counter intuitive to me 🙁