When your website means business.

«Return to Blog List Filter Custom Post Types with Dropdown Menu Using Custom Field Values

I was recently called on to allow user filtering of custom post results. WESST’s new website lists all trainings (custom post type) for all six regions, sorted by date of training. We needed to allow site visitors to show trainings for only the region they select from a dropdown menu.

NOTE: Looking for a way to filter custom post types with a custom taxonomy?

It should be noted that filtering in this example is done with custom fields, as opposed to custom taxonomies. In this case, there are a set number of regions, which won’t change, so there is no need to allow for easy creation of additional options. Using custom taxonomies is more complex because of (among other things) the need to dynamically create select options for filtering. Filtering with taxonomies also changes the queries, of course.

I’m not primarily a coder, so there may be a more concise way of doing this, but after lots of Googling, finding parts of the solution here and there, this works the way I want it to. See it in action on WESST’s website. In the meantime, if you’re a coder, feel free to contact me and show me a better way.

'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 { //if sort value exists, the query that compares sort value and custom field value      $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 '

'.$trainingregion.' Training Events

'; //heading for sorted results      } while ( $traininglist->have_posts() ) : $traininglist->the_post(); ?>

Display results here

There are no upcoming '.$trainingregion.' trainings listed.

'; //message for no result      endif; if(function_exists('wp_pagenavi')) { wp_pagenavi(array('query' => $traininglist)); } else { echo 'Please enable WP-PageNavi!'; } wp_reset_query(); ?>


Comments are closed.

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.
Cookies Notice