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 trunktrunk A directory in Subversion containing the latest development code in preparation for the next major release cycle. If you are running "trunk", then you are on the latest revision. 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 testunit test Code written to test a small piece of code or functionality within a larger application. Everything from themes to WordPress core have a series of unit tests. Also see regression. 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 bugbug A bug is an error or unexpected result. Performance improvements, code optimization, and are considered enhancements, not defects. After feature freeze, only bugs are dealt with, with regressions (adverse changes from the previous version) being the highest priority. 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 invalidinvalid A resolution on the bug tracker (and generally common in software development, sometimes also notabug) that indicates the ticket is not a bug, is a support request, or is generally 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