UPDATED January 2023 to work with PHP8.
<form action="" method="post">
<select id="region" class="postform" name="trngregion" onchange="submit();" data-ddg-inputtype="identities.addressProvince"><--establish 'sort value'-->
<option selected="selected" value="All Regions">All Regions (default)</option>
<option selected="selected" value="Albuquerque">Albuquerque</option>
<option selected="selected" value="Farmington">Farmington</option>
<option selected="selected" value="Las Cruces">Las Cruces</option>
<option selected="selected" value="Rio Rancho">Rio Rancho</option>
<option selected="selected" value="Roswell">Roswell</option>
<option selected="selected" value="Santa Fe">Santa Fe</option>
</select>
</form>
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$today = date('Ymd', strtotime('-6 hours'));
if ((empty($_POST['trngregion'])) || ($_POST['trngregion'] == 'All Regions')) { //if 'sort value' not selected
$traininglist= new WP_Query( array( //this is the basic query without region selected
'post_type' => 'training',
'posts_per_page' => 6,
'paged' => $paged,
'meta_key' => 'trng_startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array( //drop training after end date
'key' => 'trng_endate',
'meta-value' => $value,
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
)
));
} else {
$trainingregion = $_POST['trngregion']; //get 'sort value'
$traininglist= new WP_Query( array(
'post_type' => 'training',
'posts_per_page' => -1, //unlimited posts keeps paging from resetting sort
'paged' => $paged,
'meta_key' => 'trng_startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array( //drop training after end date
'key' => 'trng_endate',
'meta-value' => $value,
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
),
array( //compare 'sort value' with 'custom field value'
'key' => 'trng_region', //custom field
'value' => $trainingregion, //sort value
'type' => 'CHAR',
'compare' => '='
)
)
));
}
if ($traininglist->have_posts()) :
if ($trainingregion) {
echo '<div class="trngfilter"><h2>'.$trainingregion.' Training Events</h2></div>'; //heading for sorted results
}
while ( $traininglist->have_posts() ) : $traininglist->the_post();
Display results here
endwhile;
(include wp-navi and reset query)
endif;
?>