When your website means business.

ACF, PHP, WP Query, WP/CP | Type: PHPFilter Custom Post Type by Custom Fields

Can be found on an Evo blog post with more explanation at the link below.

<form method="post" action=""> 
      <select name="trngregion" id="region" class="postform" onchange="submit();"> <!--establish 'sort value'-->
           <option <?php echo ($_POST['trngregion'] == '') ? ' selected="selected"' : ''; ?> value="">All Regions (default)</option> 
           <option <?php echo ($_POST['trngregion'] == 'Albuquerque') ? ' selected="selected"' : ''; ?> value="Albuquerque">Albuquerque</option> 
           <option <?php echo ($_POST['trngregion'] == 'Farmington') ? ' selected="selected"' : ''; ?> value="Farmington">Farmington</option> 
           <option <?php echo ($_POST['trngregion'] == 'Las Cruces') ? ' selected="selected"' : ''; ?> value="Las Cruces">Las Cruces</option> 
           <option <?php echo ($_POST['trngregion'] == 'Rio Rancho') ? ' selected="selected"' : ''; ?> value="Rio Rancho">Rio Rancho</option> 
           <option <?php echo ($_POST['trngregion'] == 'Roswell') ? ' selected="selected"' : ''; ?> value="Roswell">Roswell</option> 
           <option <?php echo ($_POST['trngregion'] == 'Santa Fe') ? ' 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( !isset($_POST['trngregion']) || '' == $_POST['trngregion']) { //if 'sort value' not selected
           $traininglist= new WP_Query( array( //this is the basic query without region selected (the easy part)
                'post_type' => 'training', 
                'posts_per_page' => 6, 
                'paged' => $paged, 
                'meta_key' => 'trng_startdate', 
                'orderby' => 'meta_value', 
                'order' => 'ASC', 
                'meta_query' => array(
                     array( 
                     'key' => 'trng_endate', 
                     'meta-value' => $value, 
                     'value' => $today, 
                     'compare' => '>=', 
                     'type' => 'CHAR'
                      ) 
                 ) 
           ) ); 
      } else { <strong>//if sort value exists, the query that compares sort value and custom field value</strong>
      $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', //add query that compares 'sort value' with 'custom field value'
                     array( 
                     'key' => 'trng_endate', 
                     'meta-value' => $value, 
                     'value' => $today, 
                     'compare' => '>=', 
                     'type' => 'CHAR'
                     ),
                     array( //add query that compares '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

<?php 
      endwhile; 
           else : 
           echo '<p>There are no upcoming '.$trainingregion.' trainings listed.</p>'; //message for no result
      endif; 
 wp_pagenavi( array( 'query' => $traininglist ) ); 
 wp_reset_query(); 
 ?>

Reference Links

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept