Sort users by meta_value_num in users.php

9 Replies ·

  1. I’m trying to sort columns in users.php by user meta. In this case user meta is expire date that is stored as strtotime. Not sure if I make mistake and should have saved the date in different format.

    Adding the custom columns and sort for it is pretty easy using actions manage_users_custom_column and manage_users_sortable_columns. But I just don’t know how the actual sorting will work.

    In WP 4.2 they added meta_value_num support but I’m just out of ideas how would I use that in users.php.

    https://core.trac.wordpress.org/ticket/27887

  2. Sami Keijonen

    I tried the same filter but also noticed that it’s not sorting correctly. I tested basic WP_User_Query in front end using meta_value_num and that worked correctly.

    I’ll see what I can find.

  3. Justin Tadlock

    I’ll keep digging. It’s usually a matter of just finding the right hook. Everything I’ve read said to use `pre_user_query`.

  4. Sami Keijonen

    Awesome! Now I see the power of reading the core code because I could not find that hook anywhere else.