[wp-trac] Re: [WordPress Trac] #4065: useful function: merge_wp_queries()

WordPress Trac wp-trac at lists.automattic.com
Fri Mar 20 13:22:58 GMT 2009


#4065: useful function: merge_wp_queries()
----------------------------+-----------------------------------------------
 Reporter:  markjaquith     |       Owner:  anonymous  
     Type:  task (blessed)  |      Status:  new        
 Priority:  low             |   Milestone:  2.9        
Component:  Optimization    |     Version:             
 Severity:  normal          |    Keywords:  needs-patch
----------------------------+-----------------------------------------------

Comment(by tomgf):

 First step, combining the WHERE clause:

 {{{
 $q1 = new WP_Query('monthnum=10&year=2008');
 $q2 = new WP_Query('monthnum=9&year=2008');
 $merged = merge_wp_queries($q1, $q2, 'post_date_gmt', 'desc');

 function merge_wp_queries( $q1, $q2, $sortby='post_date_gmt',
 $sortorder='desc' )
 {
  global $wpdb, $merged_where;
  $where_1 = "(
 YEAR(".$wpdb->posts.".post_date)='".$q1->query_vars['year']."' AND
 MONTH(".$wpdb->posts.".post_date)='".$q1->query_vars['monthnum']."' )";
  $where_2 = "(
 YEAR(".$wpdb->posts.".post_date)='".$q2->query_vars['year']."' AND
 MONTH(".$wpdb->posts.".post_date)='".$q2->query_vars['monthnum']."' )";
  $merged_where = " AND ( $where_1 OR $where_2 )";

  add_filter('posts_where', 'merged_where' );
  return new WP_Query('showposts=10');
 }

 function merged_where( $where )
 {
  global $merged_where;
  return $where . $merged_where;
 }
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/4065#comment:3>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list