Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Query SQL Database and Display PHP (Code Shown)

Hey everyone,

I need to query a table I made in my wordpress database. The code I have works.... BUT I want it to show the data based on the current users id.... this is what I have that works but doesn't filter based on users. It just displays everything.

global $wpdb;
$results = $wpdb->get_results("SELECT meta_key FROM wp_eddress_organisations WHERE meta_value = 1 ORDER BY meta_key");

if(!empty($results)) { foreach($results as $r) {
echo "<p>".$r->meta_key."</p>"; } } else { echo "<p>Nothing found!</p>";
}

This is what I tried but failed at lol global $wpdb;
$results = $wpdb->get_results("SELECT meta_key FROM wp_eddress_organisations WHERE meta_value = 1 AND user_id = <?php get_current_user_id(); ?> ORDER BY meta_key");

if(!empty($results)) { foreach($results as $r) {
echo "<p>".$r->meta_key."</p>"; } } else { echo "<p>Nothing found!</p>";
}

1 Answer

I worked it out don't worry