Title: WP variables in custom sql-query
Last modified: December 30, 2017

---

# WP variables in custom sql-query

 *  Resolved [quazy](https://wordpress.org/support/users/quazy/)
 * (@quazy)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/)
 * Hello again.
 * I have another question: can i operate with some wordpress variables like user_id
   when I create a table with custom query to MySQL DB? I need seperate info for
   my users in WPTables.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Ian Sadovy](https://wordpress.org/support/users/iansadovy/)
 * (@iansadovy)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/#post-9833676)
 * Hello [@quazy](https://wordpress.org/support/users/quazy/),
 * Sorry for the late reply.
    Currently, there is no such option, you can use only`
   pure` SQL. But it sounds like a good idea for improvement. I will add this to
   my backlog.
 * Hope it helps.
 * Regards,
    Ian
 *  Plugin Author [Ian Sadovy](https://wordpress.org/support/users/iansadovy/)
 * (@iansadovy)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/#post-9911517)
 * Hello [@quazy](https://wordpress.org/support/users/quazy/),
 * I just have published WPTables 1.3.7 with support for MySQL variables.
    Please
   update and try predefined variable `$user_id` as following: `SELECT * FROM users
   WHERE ID = $user_id` Also, you can use `wptables_mysql_query` filter to modify
   MySQL query in your own way using PHP.
 * Please let me know if it works for you.
 * Regards,
    Ian
 *  [igvol](https://wordpress.org/support/users/igvol/)
 * (@igvol)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/#post-9960340)
 * Hello [@iansadovy](https://wordpress.org/support/users/iansadovy/),
 * I am starting to work with WordPress and WPTables.
 * Can you give me more information on using `wptables_mysql_query`?
    I need filter
   the customers dinamically as the user needs.
 * Thank you.
 * Regards,
 * Iñigo.
 *  Plugin Author [Ian Sadovy](https://wordpress.org/support/users/iansadovy/)
 * (@iansadovy)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/#post-9964327)
 * Hello [@igvol](https://wordpress.org/support/users/igvol/),
 * The main purpose of the `wptables_mysql_query` filter is to allow using custom
   variables in the MySQL query:
    1. You define the query in the admin and use some special placeholder that you 
       want to be replaced dynamically while executing. For example, if you want to
       show the table with data only for current month: `SELECT * FROM sales WHERE 
       date > $current_month`
    2. Then, you define filter in the PHP (i.e. _functions.php_) to replace $current_month
       with actual date:
    3.     ```
           add_filter('wptables_mysql_query', 'my_custom_query');
           function my_custom_query($query) {
             return str_replace('$current_month', date('Y-m-01'), $query);
           }
           ```
       
 * On the other hand, if you want to allow your users to filter the data dynamically
   on the front-end, it may be connected to the following thread – [https://wordpress.org/support/topic/custom-filter-2/](https://wordpress.org/support/topic/custom-filter-2/)
 * Hope it helps.
 * Regards,
    Ian
    -  This reply was modified 8 years, 4 months ago by [Ian Sadovy](https://wordpress.org/support/users/iansadovy/).
    -  This reply was modified 8 years, 4 months ago by [Ian Sadovy](https://wordpress.org/support/users/iansadovy/).

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘WP variables in custom sql-query’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wptables_c8eafa.svg)
 * [WordPress Tables](https://wordpress.org/plugins/wptables/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wptables/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wptables/)
 * [Active Topics](https://wordpress.org/support/plugin/wptables/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wptables/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wptables/reviews/)

## Tags

 * [$user_ID](https://wordpress.org/support/topic-tag/user_id/)
 * [current user](https://wordpress.org/support/topic-tag/current-user/)
 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [variables](https://wordpress.org/support/topic-tag/variables/)

 * 4 replies
 * 3 participants
 * Last reply from: [Ian Sadovy](https://wordpress.org/support/users/iansadovy/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/wp-variables-in-custom-sql-query/#post-9964327)
 * Status: resolved