Reputation: 13
In wordpress I'm trying to build a query that returns a list of data from a table where there is a match on a custom field I created for users that contains a reference number.
The query I have currently returns all the information from the test table, what I'd LIKE to do is return the information where the search term is the value in the custom field of the currently logged in user.
This is my current code:
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM test_table" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->customernumber.'</td>';
echo '<td>' . $print->field2.'</td>';
echo '<td>' . $print->field3.'</td>';
echo '</tr>';
}
How do I go about modifying it? What I want to do is say something along the lines of SELECT * FROM test_table WHERE customernumber = (the value of the customernumber field for the current logged in user)
Do I need to use something like get_user_meta ? I'm still kinda new to wordpress queries...
I'm not sure if I've explained this as clearly as I could, but I'm grateful for any help / advice... thanks :)
Upvotes: 0
Views: 319
Reputation: 1622
Rather than using SQL queries directly (believe me, you'll regret it in the end) use wordpress' own methods. Try something like this:
$current_user = wp_get_current_user();
$customer_number = get_user_meta($current_user->ID, "customernumber")
echo "<tr>
<td>" . $customer_number . "</td>
<td>" . $current_user->user_login . "</td>
<td>" . $current_user->ID . "</td>
</tr>";
The last two have been added in for an example purpose but you can modify that to get what you need.
Upvotes: 1