[wp-trac] [WordPress Trac] #14997: Single filter for all query bits in WP_Query

WordPress Trac wp-trac at lists.automattic.com
Thu Sep 30 18:34:51 UTC 2010


#14997: Single filter for all query bits in WP_Query
-------------------------+--------------------------------------------------
 Reporter:  scribu       |       Owner:                 
     Type:  enhancement  |      Status:  new            
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Query        |     Version:                 
 Severity:  normal       |    Keywords:                 
-------------------------+--------------------------------------------------
 From #14349:

 Most times, when you have to manipulate the raw SQL request in WP_Query,
 you need to alter multiple clauses (JOIN, WHERE etc.).

 Currently, you need to register a callback for each of those:

 {{{
 function fictional_alter_where( $where, $wp_query ) {
     if ( $wp_query->get( 'custom_var' ) ) {
         $where .= " AND meta_value IN ( 'foo', 'bar' )";
     }

     return $where;
 }
 add_filter( 'posts_where', 'fictional_alter_where' );

 function fictional_alter_join( $join, $wp_query ) {
     global $wpdb;

     if ( $wp_query->get( 'custom_var' ) ) {
         $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID =
 $wpdb->postmeta.post_id )";
     }

     return $join;
 }
 add_filter( 'posts_where', 'fictional_alter_join' );
 }}}

 I think we can all agree that this is pretty cumbersome.

 Here's how it can look like:

 {{{
 function fictional_alter_query( $bits, $wp_query ) {
     global $wpdb;

     if ( $wp_query->get( 'custom_var' ) ) {
         $bits['where'] .= " AND meta_value IN ( 'foo', 'bar' )";
         $bits['join']  .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID =
 $wpdb->postmeta.post_id )";
     }

     return $bits;
 }
 new WP_Query_Manipulation( 'fictional_alter_query' );
 }}}

 [http://core.trac.wordpress.org/attachment/ticket/14349/wp-query-
 manipulation.php WP_Query_Manipulation] is a class that leverages the
 existing hooks, but it has a lot of overhead and isn't 100% reliable.

 So, I propose we add this filter in WP_Query.

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


More information about the wp-trac mailing list