1

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.

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

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.