I need to copy certain data from wp_usermeta into a new table when a certain plugin is activated on a subsite for the first time. So far, I can get the $admin_ids to print as expected, but don’t know how or if I can use these values in an IN statement in SQL. So far, the table is not created using the below methods.
Question is, how do I write this SQL query correctly so that when the function runs, a new table is created based on the wp_usermeta table where the current blog admins’ IDs are equal to wp_usermeta.user_id.
I am not getting any errors, warnings, or notices when I run the function, wp simply stalls forever.
Here’s my code.
function create_blog_usermeta_table(){
if(function_exists('other_plugin_function')){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// WP_User_Query arguments
$args = array(
'role' => 'Administrator',
'fields' => 'ID'
);
// The User Query
$user_query = new WP_User_Query( $args );
// Get the results
$admins = $user_query->get_results();
//Extract and format admin ids from the results
$admin_ids = wp_list_pluck( $admins, 'ID', 'ID');
$admin_ids = implode( ',', $admin_ids );
$usermeta_table = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}_usermeta AS
SELECT *
FROM wp_usermeta
Where user_id IN ($admin_ids)";
}
}
2
Answers
This worked.
The previous solution did not copy indexes. This one does.