[wp-trac] [WordPress Trac] #14349: Filter to query_posts() args
    WordPress Trac 
    wp-trac at lists.automattic.com
       
    Mon Jul 19 20:48:05 UTC 2010
    
    
  
#14349: Filter to query_posts() args
-------------------------+--------------------------------------------------
 Reporter:  lucaswxp     |        Owner:            
     Type:  enhancement  |       Status:  closed    
 Priority:  normal       |    Milestone:            
Component:  Query        |      Version:  3.0       
 Severity:  normal       |   Resolution:  worksforme
 Keywords:               |  
-------------------------+--------------------------------------------------
Changes (by scribu):
  * keywords:  needs-patch =>
  * status:  new => closed
  * resolution:  => worksforme
  * milestone:  3.1 =>
Comment:
 Most times, when you have to manipulate the raw SQL request, you need to
 alter multiple sections in the query. Therefore, you need to register a
 callback for each SQL segment and repeat the same check in each callback:
 {{{
 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 you'll agree that this is pretty cumbersome.
 Using wp-query-manipulation.php:
 {{{
 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' );
 }}}
 I'm so happy I've found an easier way to do this and it doesn't even
 require a patch. Thanks, lucaswxp :D
-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/14349#comment:9>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
    
    
More information about the wp-trac
mailing list