Support » Plugin: WordPress Tables » $user_id only seems to work for admin?

  • Resolved khaihong

    (@khaihong)


    Hi, I would really appreciate your help.

    I created an sql table with the sql:

    select userid, field2, field3 from mytable where userid = $user_id

    There are 2 rows in mytable
    [userid:1, field2:a, field3:b]
    [userid:testuser, field2:c, field3:d]

    The table shows the correct first row of data if I log in as admin (WordPress default user ID for admin is “1”), but when I log in as testuser the table shows “no results found”. How can I get the second row to show when I log in as testuser. Thanks

    • This topic was modified 1 year, 3 months ago by  khaihong.
Viewing 2 replies - 1 through 2 (of 2 total)
  • I finally figured out that $user_id returns a numeric value, so it was necessary to make use of the add_filter function to get user_login instead. I hope the following helps someone:

    WPTables query:
    select userid, field2, field3 from mytable where userid = $user_login

    functions.php:
    add_filter(‘wptables_mysql_query’, ‘my_custom_query’);
    function my_custom_query($query) {
    $current_user = wp_get_current_user();
    $user_login = ‘\”.$current_user->user_login.’\”;
    return str_replace(‘$user_login’, $user_login, $query);
    }

    Now the table shows data that is specific to the logged in user.

    Plugin Author Ian Sadovy

    (@iansadovy)

    Hello @khaihong,

    Glad to hear it works for you.
    And thank you for posting your code here, I think many users will find it helpful.

    Regards,
    Ian

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$user_id only seems to work for admin?’ is closed to new replies.