[wp-trac] Re: [WordPress Trac] #2721: Add filter for wp-db query

WordPress Trac wp-trac at lists.automattic.com
Fri Sep 1 17:28:09 GMT 2006


#2721: Add filter for wp-db query
-----------------------------------+----------------------------------------
 Reporter:  filosofo               |        Owner:  anonymous
     Type:  defect                 |       Status:  new      
 Priority:  normal                 |    Milestone:  2.1      
Component:  Administration         |      Version:  2.1      
 Severity:  normal                 |   Resolution:           
 Keywords:  wp-db has-patch query  |  
-----------------------------------+----------------------------------------
Comment (by filosofo):

 Ah, the method inheritance works very nicely.  Beautiful!

 But as far as I can tell you still have to re-define the WP tables, as
 they're defined (in wp-settings.php) external to the wpdb class.

 For example, suppose you want to correct [ticket:3091 this ambiguity] like
 so:

 {{{
 class my_wpdb extends wpdb {
         function query($query) {
                 parent::query(str_replace('SELECT COUNT(DISTINCT ID)',
 "SELECT COUNT(DISTINCT $this->posts.ID)", $query));
         }
 }

 $wpdb = new my_wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
 }}}

 That by itself will produce a db error, as now your tables are undefined.
 (And hopefully there aren't any other plugins that have defined custom
 tables prior to yours).  You have to add the following:

 {{{
         $wpdb->posts            = $table_prefix . 'posts';
         $wpdb->users            = $table_prefix . 'users';
         $wpdb->categories       = $table_prefix . 'categories';
         $wpdb->post2cat         = $table_prefix . 'post2cat';
         $wpdb->comments         = $table_prefix . 'comments';
         $wpdb->links            = $table_prefix . 'links';
         $wpdb->linkcategories   = $table_prefix . 'linkcategories';
         $wpdb->options          = $table_prefix . 'options';
         $wpdb->postmeta         = $table_prefix . 'postmeta';
         $wpdb->usermeta         = $table_prefix . 'usermeta';
         $wpdb->prefix           = $table_prefix;
 }}}

 All of which seems more cumbersome than something like

 {{{
 add_filter('query', create_function('$q', 'global $wpdb; return
 str_replace("SELECT COUNT(DISTINCT ID)", "SELECT COUNT(DISTINCT
 $wpdb->posts.ID)",$q);'));
 }}}

-- 
Ticket URL: <http://trac.wordpress.org/ticket/2721>
WordPress Trac <http://wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list