Skip to content

Instantly share code, notes, and snippets.

@jackabox
Created May 14, 2015 11:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackabox/a2b48c780908adbda4bc to your computer and use it in GitHub Desktop.
Save jackabox/a2b48c780908adbda4bc to your computer and use it in GitHub Desktop.
Filtering custom post types by an ACF field.

Filtering posts via an ACF field.

  • Define the acf key
  • grab the choices
  • create a select with choices
  • request on the meta key/meta value
function my_restrict_manage_posts() {
global $typenow;
if ($typenow == 'adtrak_applicants'){
$field_key = "field_553fafc094dc6";
$field = get_field_object($field_key)['choices'];
?>
<select name="adtrak_applicants_filter">
<option value=""><?php _e('All statuses', 'adtrak_applicants'); ?></option>
<?php
$current_v = isset($_GET['adtrak_applicants_filter']) ? $_GET['adtrak_applicants_filter'] : '';
foreach ($field as $label => $value) {
printf (
'<option value="%s"%s>%s</option>',
$value,
$value == $current_v? ' selected="selected"':'',
$label
);
}
?>
</select>
<?php
}
}
add_action('restrict_manage_posts','my_restrict_manage_posts');
function request_applicant_filter($request) {
if (is_admin() && isset($request['post_type']) && $request['post_type']=='adtrak_applicants') {
$request['meta_key'] = 'status';
$request['meta_value'] = $_GET['adtrak_applicants_filter'];
}
return $request;
}
add_action( 'request', 'request_applicant_filter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment