After reading the great article on Ractoon.com on how to create sortable admin columns for custom posts, I decided I wanted to take things a step further and add a select box to the top of the posts table to filter by a custom field.
Here’s the code. I followed the same naming conventions as on Ractoon
add_action('restrict_manage_posts', 'filter_posts_by_mycpt'); function filter_posts_by_mycpt() { global $typenow; $acf_field_id = $_GET['acf_field']; //TODO: sanitize $_GET vars if ($typenow !== 'mycpt'){ return; } ?> <select name="acf_field"> <option value="0">All acf_fields</option> <?php // Get all possible values the field can have. // In my case these are Events created by The Events // calendar plugin. But it can be anything really. $acf_fields = tribe_get_events(); foreach($acf_fields as $acf_field) { ?> <option <?php echo $acf_field_id == $acf_field->ID ? 'selected' : '' ?> value="<?php echo $acf_field->ID ?>" > <?php echo get_the_title($acf_field->ID) ?> </option> <?php } ?> </select> <?php } add_filter('parse_query', 'parse_query_filter_mycpt'); function parse_query_filter_mycpt($query) { global $pagenow; $post_type = 'mycpt'; // change to your post type $q_vars = &$query->query_vars; $acf_field_id = $_GET['acf_field']; if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && tribe_is_event($acf_field_id) ) { $q_vars['meta_key'] = 'acf_field'; $q_vars['meta_value'] = $acf_field_id; } }