How to get all the users list after you performed the hook "pre_get_users"?

admin2025-01-08  3

I have a custom filter called user type in the admin user dashboard and I have already worked it out to display them in the user admin dashboard. But I don't know how to call and save them in an array so that I can get the user_ids and perform another action.

My other action would be to get all the meta keys of a certain user and I would like to export them into an excel file. But before I can do that I should have an option to filter them or just export all the users, which I have already performed it.

function add_course_section_filter() {
    if ( isset( $_GET[ 'user_type' ]) ) {
        $user_type = $_GET[ 'user_type' ];

    } else {
        $user_type = '';
    }

    echo ' <select name="user_type[]" style="float:none;"><option value="">Choose User Type...</option>';

      if($user_type[0] == 'designer'){
        $selected_designer = 'selected="selected"';

      }

      if($user_type[0] == 'client'){
        $selected_client = 'selected="selected"';

      }
      echo '<option value="designer" ' . $selected_designer . '>Builder/Designer</option>';
      echo '<option value="client" ' . $selected_client . '>Client</option>';
    echo '</select>';

    echo '<input type="submit" class="button" value="Filter">';
}
add_action( 'restrict_manage_users', 'add_course_section_filter' );

function filter_users_by_course_section( $query ) {
    global $pagenow;

    if ( is_admin() && 'users.php' == $pagenow && isset( $_GET[ 'user_type' ]  )) {
        $user_type = $_GET[ 'user_type' ];

        if($user_type[0] != ''){
          $meta_query = array(
              array(
                  'key' => '_user_type',
                  'value' => $user_type[0]
              )
          );
          $query->set( 'meta_key', '_user_type' );
          $query->set( 'meta_query', $meta_query );
        }else{

        }
    }

}
add_filter( 'pre_get_users', 'filter_users_by_course_section' );

I have a custom filter called user type in the admin user dashboard and I have already worked it out to display them in the user admin dashboard. But I don't know how to call and save them in an array so that I can get the user_ids and perform another action.

My other action would be to get all the meta keys of a certain user and I would like to export them into an excel file. But before I can do that I should have an option to filter them or just export all the users, which I have already performed it.

function add_course_section_filter() {
    if ( isset( $_GET[ 'user_type' ]) ) {
        $user_type = $_GET[ 'user_type' ];

    } else {
        $user_type = '';
    }

    echo ' <select name="user_type[]" style="float:none;"><option value="">Choose User Type...</option>';

      if($user_type[0] == 'designer'){
        $selected_designer = 'selected="selected"';

      }

      if($user_type[0] == 'client'){
        $selected_client = 'selected="selected"';

      }
      echo '<option value="designer" ' . $selected_designer . '>Builder/Designer</option>';
      echo '<option value="client" ' . $selected_client . '>Client</option>';
    echo '</select>';

    echo '<input type="submit" class="button" value="Filter">';
}
add_action( 'restrict_manage_users', 'add_course_section_filter' );

function filter_users_by_course_section( $query ) {
    global $pagenow;

    if ( is_admin() && 'users.php' == $pagenow && isset( $_GET[ 'user_type' ]  )) {
        $user_type = $_GET[ 'user_type' ];

        if($user_type[0] != ''){
          $meta_query = array(
              array(
                  'key' => '_user_type',
                  'value' => $user_type[0]
              )
          );
          $query->set( 'meta_key', '_user_type' );
          $query->set( 'meta_query', $meta_query );
        }else{

        }
    }

}
add_filter( 'pre_get_users', 'filter_users_by_course_section' );
Share Improve this question edited Apr 30, 2018 at 7:28 Felicisimo Barbosa Vasquez Jr. asked Apr 30, 2018 at 6:19 Felicisimo Barbosa Vasquez Jr.Felicisimo Barbosa Vasquez Jr. 12 bronze badges 2
  • which another action do you want to do ? edit your question to give a example of result you want. – mmm Commented Apr 30, 2018 at 7:06
  • Hi thank you for replying. My other action would be to get all the meta keys of a certain user and I would like to export them into an excel file. But before I can do that I should have an option to filter them or just export all the users, which I have already performed it. – Felicisimo Barbosa Vasquez Jr. Commented Apr 30, 2018 at 7:26
Add a comment  | 

1 Answer 1

Reset to default 0

with you code, the user list is filtered by the meta "_user_type".
then you can create a new bulk action to export data of the selected users with that :

const ACTION_EXPORT_CSV = "MyPlugin__exportCSV";


add_filter("bulk_actions-users", function ($actions) {

    $actions[ACTION_EXPORT_CSV] = "Export CSV";

    return $actions;

});


add_action("load-users.php", function () {

    if (    isset($_GET["action"])
        &&  (ACTION_EXPORT_CSV === $_GET["action"])
    ) {

        // here, identifiers of selected users are in $_GET["users"]


    }


});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265771a1068.html

最新回复(0)