How can I simultaneously query all blog options table in a Wordpress multisite installation (3.0)? How can I simultaneously query all blog options table in a Wordpress multisite installation (3.0)? wordpress wordpress

How can I simultaneously query all blog options table in a Wordpress multisite installation (3.0)?


In case anyone is interested, I ended up doing it like this (but I would still like to know if its possible to do a search on table names using LIKE and then query those tables, if anyone knows).

// so get all the blog ids from the blogs table$blogs = $wpdb->get_results("SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A);// build a sql statement for each blog options table, adding in the blog id for each row$select_statements = array();foreach ($blogs as $blog_row) {    $select_statements[] = 'SELECT option_value, CAST( '.$blog_row['blog_id'].' AS UNSIGNED INTEGER ) AS blog_id FROM '.$wpdb->get_blog_prefix($blog_row['blog_id'])."options WHERE option_name='$option_name'";}// cache the results of the union of all these select statements$option_results = $wpdb->get_results(implode(' UNION ALL ', $select_statements), ARRAY_A);


If you want to query directly MySQL database, you can create a procedure and use it:

use wordpress;Drop Procedure IF EXISTS wordpress.MyProcedure;DELIMITER | ;CREATE PROCEDURE MyProcedure (param1 VARCHAR(30))BEGIN        DECLARE tbname CHAR(50);        DECLARE endfetch INT DEFAULT 0;        DECLARE cur1 CURSOR FOR         SELECT table_name FROM information_schema.tables WHERE table_schema='wordpress' and table_name like '%options';        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'                SET endfetch = 1;        OPEN cur1;        FETCH cur1 INTO tbname;        fetchloop: WHILE NOT endfetch DO            SELECT tbname ;             SET @opt = param1;            SET @table_name = tbname;            SET @sql_text = concat('SELECT option_value FROM ',@table_name,' WHERE option_name=''',@opt,'''');            PREPARE stmt FROM @sql_text;            EXECUTE stmt;            DEALLOCATE PREPARE stmt;            FETCH cur1 INTO tbname;        END WHILE fetchloop;END|DELIMITER ; |CALL MyProcedure('siteurl');