How to write query for multisite use
-
I’m trying to create a plugin for use on our multisite install, but I can’t quite figure out how to the db query in a way that will automatically get data from the correct subsite’s db tables.
Here’s a query that works:
$reportList = $wpdb->get_results($wpdb->prepare("SELECT wp_2_watupro_taken_exams.ID, wp_2_watupro_taken_exams.exam_id, wp_2_watupro_taken_exams.user_id, wp_2_watupro_taken_exams.end_time 'completed', wp_2_watupro_master.name 'assessment', wp_users.display_name 'name', wp_users.user_email 'email' FROM wp_2_watupro_taken_exams JOIN wp_2_watupro_master ON wp_2_watupro_master.ID = wp_2_watupro_taken_exams.exam_id JOIN wp_users ON wp_users.ID = wp_2_watupro_taken_exams.user_id WHERE wp_users.user_email LIKE '%".$_POST['client']."%' ORDER BY wp_users.display_name;"));
I want to make it so that I don’t have the “wp_2_” in there — i.e., so that it will just automatically get the results based on the right prefix for the current site.
I’ve done a bunch of searching and reading, and here’s what I’ve got (which doesn’t work):
$reportList = $wpdb->get_results($wpdb->prepare("SELECT $wpdb->watupro_taken_exams.ID, $wpdb->watupro_taken_exams.exam_id, $wpdb->watupro_taken_exams.user_id, $wpdb->watupro_taken_exams.end_time 'completed', $wpdb->watupro_master.name 'assessment', $wpdb->users.display_name 'name', $wpdb->users.user_email 'email' FROM $wpdb->watupro_taken_exams JOIN $wpdb->watupro_master ON $wpdb->watupro_master.ID = $wpdb->watupro_taken_exams.exam_id JOIN $wpdb->users ON $wpdb->users.ID = $wpdb->watupro_taken_exams.user_id WHERE $wpdb->users.user_email LIKE '%".$_POST['client']."%' ORDER BY $wpdb->users.display_name;"));
So, the question is – where have I gone wrong?
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to write query for multisite use’ is closed to new replies.