Title: show data table by user id
Last modified: August 30, 2016

---

# show data table by user id

 *  [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/)
 * Hi,
 * I have created a table, with ID, userID, colm1, colm2 where userID is Foreign
   key to the ID colmn in the wp_users table.
 * I want to show the data by the login user, So every logged user can see his information
   only. (like this where ID=userID ) wp_get_current_user()
 * thank you.
 * [https://wordpress.org/plugins/custom-database-tables/](https://wordpress.org/plugins/custom-database-tables/)

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

 *  Plugin Author [ka2](https://wordpress.org/support/users/ka2/)
 * (@ka2)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521806)
 * Thank you for your inquiry.
 * It will be possible to do by following.
 * 1. Firstly, it’ll add a filter into 909th line of plugin core class (lib/cdbt.
   class.php).
 *     ```
       $where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );
       ```
   
 * 2. Next, please enabled a filter by “functions.php” of your theme into the user
   access page (post to use the shortcode cdbt-view).
 *     ```
       function custom_get_data_conditions( $where_clause, $table_name, $conditions ){
         $current_user = wp_get_current_user();
   
         if ('your_created_table' === $table_name) {
           if (empty($conditions)) {
             $where_clause = sprintf( "WHERE userID = %d ", $current_user->ID);
           } else {
             $where_clause .= sprintf( "AND userID = %d ", $current_user->ID);
           }
         }
         return $where_clause;
       }
       add_filter( 'cdbt_get_data_conditions', 'custom_get_data_conditions', 10, 3);
       ```
   
 * Please try by all means.
    Thank you,
 *  Thread Starter [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521889)
 * Hi I tried but nothing changed.
 * I placed the where clause to 909th line, and add the filter in the Function ,
   but nothing changed.
 *  Plugin Author [ka2](https://wordpress.org/support/users/ka2/)
 * (@ka2)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521897)
 * Hmm…that’s troubling, isn’t it.
 * Please confirm one point.
    Do you specify a table name that you want to filter
   the table name in the condition of “if” statement on the source that you have
   added to “functions.php” in your theme?
 * If there is no problem in the above table name specification, please let me know
   the results of the debug code below.
 *     ```
       function custom_get_data_conditions( $where_clause, $table_name, $conditions ){
         $current_user = wp_get_current_user();
   
         if ('your_created_table' === $table_name) {
           if (empty($conditions)) {
             $where_clause = sprintf( "WHERE userID = %d ", $current_user->ID);
           } else {
             $where_clause .= sprintf( "AND userID = %d ", $current_user->ID);
           }
         }
         var_dump($where_clause);
         return $where_clause;
       }
       add_filter( 'cdbt_get_data_conditions', 'custom_get_data_conditions', 10, 3);
       ```
   
 * By this code, I have put a debugging code at “var_dump” to display the variable
   of `$where_clause` before that is returned.
 * Thank you,
 *  Thread Starter [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521898)
 * I dont see any changes, where can i see the debugging code result ?
 * Is there a way to contact you privately ?
 *  Thread Starter [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521899)
 * I want to make sure is this the place where i place the filter in the 909th line
   in cdbt.class ?
 *     ```
       if (!empty($client_host)) {
   
       					list($client_addr, ) = gethostbynamel($client_host);
       				} else {
       					$where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );
   
       					$client_addr = $_SERVER['SERVER_ADDR'];
   
       				}
       ```
   
 *  Plugin Author [ka2](https://wordpress.org/support/users/ka2/)
 * (@ka2)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521907)
 * Oh, It differences is insertion place of the filter.
    Did you use plugin versions
   1.1.15?
 * The filter needs to be added to the get_data within the method of cdbt.class.
   php.
    The right place is as follows:
 *     ```
       function get_data($table_name, $columns='*', $conditions=null, $order=array('created'=>'desc'), $limit=null, $offset=null) {
       	global $wpdb;
       	list(, , $table_schema) = $this->get_table_schema($table_name);
       	$select_clause = is_array($columns) ? implode(',', $columns) : (!empty($columns) ? $columns : '*');
       	$where_clause = $order_clause = $limit_clause = null;
       	if (!empty($conditions)) {
       		$i = 0;
       		foreach ($conditions as $key => $val) {
       			if (array_key_exists($key, $table_schema)) {
       				if ($i == 0) {
       					$where_clause = "WHERE <code>$key</code> = '$val' ";
       				} else {
       					$where_clause .= "AND <code>$key</code> = '$val' ";
       				}
       				$i++;
       			} else {
       				continue;
       			}
       		}
       	}
       $where_clause = apply_filters( 'cdbt_get_data_conditions', $where_clause, $table_name, $conditions );
       	if (!empty($order)) {
       		$i = 0;
       		foreach ($order as $key => $val) {
       			if (array_key_exists($key, $table_schema)) {
       				$val = strtoupper($val) == 'DESC' ? 'DESC' : 'ASC';
       				if ($i == 0) {
       					$order_clause = "ORDER BY <code>$key</code> $val ";
       				} else {
       					$order_clause .= ", <code>$key</code> $val ";
       				}
       				$i++;
       			} else {
       				continue;
       			}
       		}
       	}
       	if (!empty($limit)) {
       		$limit_clause = "LIMIT ";
       		$limit_clause .= (!empty($offset)) ? intval($offset) .', '. intval($limit) : intval($limit);
       	}
       	$sql = sprintf(
       		"SELECT %s FROM <code>%s</code> %s %s %s",
       		$select_clause,
       		$table_name,
       		$where_clause,
       		$order_clause,
       		$limit_clause
       	);
       	return $wpdb->get_results($sql);
       }
       ```
   
 * After correct the position of the filter, please try again.
 *  Thread Starter [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521913)
 * Thank you Very Very Much, It is working Now 😀
 *  Thread Starter [alsayyed](https://wordpress.org/support/users/alsayyed/)
 * (@alsayyed)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6521914)
 * Now in insert data, how to get the userID of the current user and insert it in
   the userID field automatically?
 *  [lisastillwell](https://wordpress.org/support/users/lisastillwell/)
 * (@lisastillwell)
 * [10 years ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6522012)
 * How can this be done in Version: 2.0.12?
 * Thanks!
 *  Plugin Author [ka2](https://wordpress.org/support/users/ka2/)
 * (@ka2)
 * [10 years ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6522013)
 * In the latest version, it is as follows.
 * e.g. shortcode:
 *     ```
       [cdbt-view table="your_created_table"]
       ```
   
 * e.g. filter hook (as like “functions.php” in your theme):
 *     ```
       function custom_filter_get_data_sql( $sql, $table_name, $sql_clauses ) {
         if ( ! is_admin() && 'your_created_table' === $table_name ) {
           $_current_user_id = 0; // For guest user
           if ( is_user_logged_in() ) {
             $current_user = wp_get_current_user();
             $_current_user_id = $current_user->ID;
           }
           $_new_sql = <<<SQL
       SELECT %s
       FROM %s
       WHERE user_id=%s
       %s %s
       SQL;
       	$sql = sprintf( $_new_sql, $sql_clauses[0], $table_name, $_current_user_id, $sql_clauses[2], $sql_clauses[3] );
         }
         return $sql;
       }
       add_filter( 'cdbt_crud_get_data_sql', 'custom_filter_get_data_sql', 10, 3 );
       ```
   
 * Thank you,

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

The topic ‘show data table by user id’ is closed to new replies.

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

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [wp_get_current_user](https://wordpress.org/support/topic-tag/wp_get_current_user/)

 * 10 replies
 * 3 participants
 * Last reply from: [ka2](https://wordpress.org/support/users/ka2/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/show-data-table-by-user-id/#post-6522013)
 * Status: not resolved