Skip to content

Instantly share code, notes, and snippets.

@FerFuego
Created October 22, 2020 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FerFuego/a32d010b57d4b9288e5c5e4ec510ceca to your computer and use it in GitHub Desktop.
Save FerFuego/a32d010b57d4b9288e5c5e4ec510ceca to your computer and use it in GitHub Desktop.
Add filter to Admin Menu
<?php
/**
* Add filter to Admin Menu
* Change: my_custom_post_type_1, my_custom_post_type_2, my_meta_type, my_meta_value and options of select
*/
add_filter( 'parse_query', 'acf_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'acf_admin_posts_filter_restrict_manage_posts' );
function acf_admin_posts_filter( $query ) {
global $pagenow;
if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
function acf_admin_posts_filter_restrict_manage_posts() {
if (isset($_GET['post_type']) && post_type_exists($_GET['post_type']) && in_array(strtolower($_GET['post_type']), array('my_custom_post_type_1', 'my_custom_post_type_2'))) : ?>
<input type="hidden" name="ADMIN_FILTER_FIELD_NAME" value="my_meta_type" />
<select name="ADMIN_FILTER_FIELD_VALUE">
<option value="">Filter by My Custom Meta Value</option>
<option value="">All</option>
<option value="my_custom_meta_value">my_custom_meta_value</option>
<option value="my_custom_meta_value_2">my_custom_meta_value_2</option>
</select>
<?php
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment