我在默认用户列表页面中显示一个名为company name
的自定义列。现在,我想按列名company name
过滤该用户列表页面。我如何添加自定义过滤器(meta_key
之一)以使用我的列过滤默认的用户列表。
请更清楚地找到图片 -
//Code to filter user list by Status
function admin_users_filter( $query ){
global $pagenow,$wp_query;
if ( is_admin() && $pagenow=='users.php' && isset($_GET['abc']) && $_GET['abc'] != '') {
$query->search_term = urldecode($_GET['abc']);
global $wpdb;
if (!is_null($query->search_term)) {
$query = $wpdb->get_results( " SELECT DISTINCT account_status FROM wp_custom_user_details ORDER BY account_status ASC " );
}
}
}
add_filter( 'pre_user_query', 'admin_users_filter' );
add_action( 'restrict_manage_users', 'restrict_abc_manage_list' );
function restrict_abc_manage_list()
{
?>
<select name="abc" style="float: none;">
<option value=""><?php _e('Filter By Status', 'baapf'); ?></option>
<option value="1">Active</option>
<option value="2">Inactive</option>
</select>
<input id="post-query-submit" class="button" type="submit" value="Filter" name="">
<?php
}
答案 0 :(得分:5)
先生。 Eek已经给出了在用户列表中添加自定义列的解决方案。所以我给出了如何为'公司'选项添加过滤器。代码是
function admin_users_filter( $query ){
global $pagenow,$wp_query;
if ( is_admin() && $pagenow=='users.php' && isset($_GET['abc']) && $_GET['abc'] != '') {
$query->search_term = urldecode($_GET['abc']);
global $wpdb;
if (!is_null($query->search_term)) {
$query->query_from .= " INNER JOIN {$wpdb->usermeta} ON " .
"{$wpdb->users}.ID={$wpdb->usermeta}.user_id AND " .
"{$wpdb->usermeta}.meta_key='meta_key_name_here' AND "."{$wpdb->usermeta}.meta_value LIKE '%{$query->search_term}%'";
}
}
}
add_filter( 'pre_user_query', 'admin_users_filter' );
add_action( 'restrict_manage_users', 'restrict_abc_manage_list' );
function restrict_abc_manage_list()
{
?>
<select name="abc" style="float: none;">
<option value=""><?php _e('Filter By College', 'baapf'); ?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input id="post-query-submit" class="button" type="submit" value="Filter" name="">
<?php
}
发现任何疑问然后问... thxs
答案 1 :(得分:0)
我正在使用admin_init挂钩来加载一个函数,该函数创建一个用于在admin中添加排序列的类:)将它放在主题文件中的functions.php中。
if(!function_exists("load_sortable_user_meta_columns")){
add_action('admin_init', 'load_sortable_user_meta_columns');
function load_sortable_user_meta_columns(){
//THIS IS WHERE YOU ADD THE meta_key => display-title values, you can add multiple args, ex: array('company'=>'Company', 'user_registered'=>'Date Registered');
$args = array('company'=>'Company');
new sortable_user_meta_columns($args);
}
}
if(!class_exists("sortable_user_meta_columns")):
class sortable_user_meta_columns{
var $defaults = array('nicename', 'email', 'url', 'registered','user_nicename', 'user_email', 'user_url', 'user_registered','display_name','name','post_count','ID','id','user_login');
//By things in user_meta
function __construct($args){
$this->args = $args;
add_action('pre_user_query', array(&$this, 'query'));
add_action('manage_users_custom_column', array(&$this, 'content'), 10, 3);
add_filter('manage_users_columns', array(&$this, 'columns'));
add_filter( 'manage_users_sortable_columns', array(&$this, 'sortable') );
}
function query($query){
$vars = $query->query_vars;
if(in_array($vars['orderby'], $this->defaults)) return;
$title = $this->args[$vars['orderby']];
if(!empty($title)){
$query->query_from .= " LEFT JOIN bib_usermeta m ON (bib_users.ID = m.user_id AND m.meta_key = '$vars[orderby]')";
$query->query_orderby = "ORDER BY m.meta_value ".$vars['order'];
}
}
function columns($columns) {
foreach($this->args as $key=>$value){
$columns[$key] = $value;
}
return $columns;
}
function sortable($columns){
foreach($this->args as $key=>$value){
$columns[$key] = $key;
}
return $columns;
}
function content($value, $column_name, $user_id) {
$user = get_userdata( $user_id ); //Get the content of the custom meta
return $user->$column_name; //Get the custom meta. Eg: Company
}
}
endif;
如果meta_key是'company',如果它是'company_name'你必须在$ args中更改它,那么这个例子就可以了。例如:$args = array('company_name' => 'Company Name');
:)
答案 2 :(得分:0)
小心点。 “restrict_manage_users”挂钩在WordPress4.4或更高版本中无法正常工作。 (最新的4.8中存在同样的问题)
解决方案如下: http://www.linsoftware.com/using-the-restrict_manage_users-action-hook-in-wordpress-4-4/