Update on Query improvements in 4.1

A few weeks ago, I posted about planned Query improvements for WP 4.1. Most of the proposed improvements (and more!) are currently in trunk and are ready for testing by developers and others who are familiar with these query classes. A summary of changes can be found below.

WP_Meta_Query

WP_Meta_Query has been pretty much rewritten, resulting in extensive improvements.

  • Improved unit test coverage #29560
  • Support for nested queries #29642. Let’s say you have posts with ‘city’ and ‘state’ metadata, and you want to fetch all items that match either city=Miami&state=Ohio or city=Augusta&state=Maine. The syntax looks like this:
    $query = new WP_Query( array(
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'relation' => 'AND',
    			array(
    				'key' => 'city',
    				'value' => 'Miami',
    			),
    			array(
    				'key' => 'state',
    				'value' => 'Ohio',
    			),
    
    		),
    		array(
    			'relation' => 'AND',
    			array(
    				'key' => 'city',
    				'value' => 'Augusta',
    			),
    			array(
    				'key' => 'state',
    				'value' => 'Maine',
    			),
    
    		),
    	),
    ) );
    		
  • Avoid redundant JOINs when using relation=OR #24093. In cases where you’re doing a particularly complex meta_query, this change could result in significant performance gains. This particular fix enabled a complete rewrite (and radical simplification) of the way WP_Meta_Query generates its SQL queries.
  • Fixed ‘orderby=meta_value’ in WP_Query when passing a ‘meta_query’ with relation=OR #25538
  • Fixed a bug that caused ‘NOT EXISTS’ queries to miss posts that have no metadata at all #29062

WP_Tax_Query

  • Improved unit test coverage #29718
  • Support for nested queries #29738. Syntax is the same as WP_Meta_Query above.
  • Avoid redundant JOINs when using relation=OR #18105
  • Added support for ‘EXISTS’ and ‘NOT EXISTS’ #29181. This means, for example, that you can fetch all posts that have no post_tags.

WP_Date_Query

  • Improved unit test coverage #29781
  • Support for nested queries #29822. Syntax is the same as WP_Meta_Query above.
  • Fix a bug that caused queries to fail when a date_query was used along with a tax_query or meta_query, due to a missed table join #25775. A side effect of this change is that the wily developer can also use WP_Date_Query to generate WHERE SQL for cross-table queries; see #29823
  • Throw _doing_it_wrong() errors when passing invalid dates #25834
  • Add support for querying by the ‘user_registered’ column in WP_User_Query #27283
  • Improve ‘inclusive’ logic when using string values for ‘before’ and ‘after’ #29908

WP_Comment_Query

  • Introduce 'include_unapproved' argument, and use it, rather than a direct SQL query, to fetch comments in comments_template() #19623
  • 'comment__in', 'comment__not_in', 'post__in', 'post__not_in' params #25386, #29189
  • 'author__in', 'author__not_in', 'post_author__in', 'post_author__not_in' params #29885

#4-1, #query