Filter users by custom colum not meta key

I want to add custom filter with a custom column from users table not a metaKey. I find this function to filter by a met

I want to add custom filter with a custom column from users table not a metaKey. I find this function to filter by a metaKey:

add_filter('pre_get_users', 'filter_users_by_job_role_section');

function filter_users_by_job_role_section($query)
{
 global $pagenow;
 if (is_admin() && 'users.php' == $pagenow) {
  // figure out which button was clicked. The $which in filter_by_job_role()
  $top = $_GET['job_role_top'];
  $bottom = $_GET['job_role_bottom'];
  if (!empty($top) OR !empty($bottom))
  {
   $section = !empty($top) ? $top : $bottom;

   // change the meta query based on which option was chosen
   $meta_query = array (array (
      'key' => 'membership_user_role',
      'value' => $section,
      'compare' => 'LIKE'
   ));

 $query->set('meta_query', $meta_query);

  }
 }
}

I want to change the query to use a comumn table "user_status". How can I do it please.

I want to add custom filter with a custom column from users table not a metaKey. I find this function to filter by a metaKey:

add_filter('pre_get_users', 'filter_users_by_job_role_section');

function filter_users_by_job_role_section($query)
{
 global $pagenow;
 if (is_admin() && 'users.php' == $pagenow) {
  // figure out which button was clicked. The $which in filter_by_job_role()
  $top = $_GET['job_role_top'];
  $bottom = $_GET['job_role_bottom'];
  if (!empty($top) OR !empty($bottom))
  {
   $section = !empty($top) ? $top : $bottom;

   // change the meta query based on which option was chosen
   $meta_query = array (array (
      'key' => 'membership_user_role',
      'value' => $section,
      'compare' => 'LIKE'
   ));

 $query->set('meta_query', $meta_query);

  }
 }
}

I want to change the query to use a comumn table "user_status". How can I do it please.

Share Improve this question edited Oct 7, 2019 at 15:25 wafa_zitouna asked Oct 7, 2019 at 15:16 wafa_zitounawafa_zitouna 112 bronze badges 1
  • Can you please clarify? You first ask how to use a custom column, but then ask how to use a custom table. – WebElaine Commented Oct 7, 2019 at 15:37
Add a comment  | 

1 Answer 1

Reset to default 1

This isn't possible with custom table columns, and you should avoid and move away from custom modifications to WP Core tables. Instead, store this data in user meta instead of custom table columns.

When you next update WordPress, if the database schema will change, WP will adjust the tables to match, destroying your custom columns and all the data they contain ( or the update will fail ).

The API you attempted to use was intended for these user meta values, you can adjust user meta just like post meta:

  • add_user_meta
  • update_user_meta
  • get_user_meta
  • delete_user_meta

You can simplify your code by using these instead, it will allow you to eliminate the raw SQL needed to adjust the table and save/fetch information, and allow you to use WP APIs as intended.

And remember, never modify WP Core to change WP, that includes the table columns.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101829a4611320.html

相关推荐

  • Filter users by custom colum not meta key

    I want to add custom filter with a custom column from users table not a metaKey. I find this function to filter by a met

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信