user2208245
user2208245

Reputation: 35

Get results from WPDB

I have this scenario that I can't figure out:

Inside table wp_comment I need to list all user_id (not duplicate) with comment_type=complete.

I tried this:

$results = $GLOBALS['wpdb']->get_results( "SELECT * FROM wp_comments WHERE comment_type='sensei_course_status' AND comment_approved='complete'", ARRAY_A );

$corsisti = $results[user_id];

// I need to print only ids to put this array in
get_users( include=> '$corsisti' )

The database screenshot:

screenshot of database

Upvotes: 3

Views: 236

Answers (1)

Zoli Szabó
Zoli Szabó

Reputation: 4534

You can use the wpdb::get_col() method to retrieve an array with values from a single column:

$corsisti = $GLOBALS['wpdb']->get_col( "SELECT `user_id` FROM wp_comments WHERE comment_type='sensei_course_status' AND comment_approved='complete'");

Then simply use the result in get_users (you do not need the quotes):

$users = get_users( include=> $corsisti );

Upvotes: 2

Related Questions