Skip to content

Instantly share code, notes, and snippets.

@imran-khan1
Last active July 5, 2019 07:01
Show Gist options
  • Save imran-khan1/3e1ffc1decbd7593e84c3bc856a4fa6e to your computer and use it in GitHub Desktop.
Save imran-khan1/3e1ffc1decbd7593e84c3bc856a4fa6e to your computer and use it in GitHub Desktop.
<?php
function ci_admin_posts_filter( $query )
{
global $pagenow;
if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_SEARCH_FIELD_NAME']) && $_GET['ADMIN_SEARCH_FIELD_NAME'] != '') {
$query->query_vars['meta_value'] = $_GET['ADMIN_SEARCH_FIELD_NAME'];
}
}
add_filter( 'parse_query', 'ci_admin_posts_filter' );
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ci_admin_posts_filter_restrict_manage_posts()
{
/** Ensure this is the correct Post Type*/
if($_GET['post_type'] !== 'product')
return;
global $wpdb;
$sql = 'SELECT post_title FROM '.$wpdb->posts.' where post_type="manufacturer" ORDER BY post_title asc';
$fields = $wpdb->get_results($sql, ARRAY_N);
?>
<select name="ADMIN_SEARCH_FIELD_NAME">
<option value=""><?php _e('Search By Manufacturers', 'ci'); ?></option>
<?php
$current = isset($_GET['ADMIN_SEARCH_FIELD_NAME'])? $_GET['ADMIN_SEARCH_FIELD_NAME']:'';
foreach ($fields as $field) {
if (substr($field[0],0,1) != "_"){
printf
(
'<option value="%s"%s>%s</option>',
$field[0],
$field[0] == $current? ' selected="selected"':'',
$field[0]
);
}
}
?>
</select>
<?php
}
add_action( 'restrict_manage_posts', 'ci_admin_posts_filter_restrict_manage_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment