Tag: wp41

WP 4.1: Towards improved Queries

Today, I had a look into the latest developments for the next WordPress version 4.1 and there are some heavy changes going on! Boone Georges works on the central classes WP_Meta_Query, WP_Tax_Query and WP_Date_Query. So basically the way, you list your posts in WordPress will be reshaped and you get more possibilities using these classes.

Multirelational and nested

The most important change in my eyes: You will be able to create nested and multi-relational queries. What does this mean? Boone Georges give us an example:

$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',
            ),
 
        ),
    ),
) );

This meta query consists out of two different arrays, which are related by “OR”. Each of this arrays now contains again two separate arrays, which in this case are related by “AND”. The first one is displaying all posts which contain in the meta_key “city” the value “Miami” and in the meta_key “state” the value “Ohio”. The second one is displaying all posts, which contain the city “Augusta” and the state “Maine”. And these two arrays now are related by OR. This WP_Meta_Query will output all posts which either contain Miami/Ohio or Augusta/Maine.

This is quite a boost in possibilities because until now, we where just able to create a single relationship between meta values. Now, you will be able to create countless relationships.

Reduced SQL

Sometimes, when you design a huge query, WordPress creates quite a complex SQL code. Sometimes this code is unnecessarily complex. For this issue, there will be some changes to create a more simple – and this means also quicker – SQL code. Take for example

array(
  'tax_query' => array(
    'relation' => 'OR',
    array('taxonomy' => 'tax1', 'field' => 'slug', 'terms' => 'term1'),
    array('taxonomy' => 'tax2', 'field' => 'slug', 'terms' => 'term2'),
  )
)

which results in

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts  
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) 
WHERE 1=1 AND
...
AND (wp_term_relationships.term_taxonomy_id IN (XXX) 
  OR tt1.term_taxonomy_id IN (YYY) ) 
...

while it could be rewritten like

SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts  
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 AND
...
AND (wp_term_relationships.term_taxonomy_id IN (XXX, YYY) ) 
...

Not only the taxonomy SQL will change for performance reasons. Also the meta query will enjoy some improvements, as the issue 24093 will be addressed with the new version:

When multiple clauses are passed to WP_Meta_Query under the scope of an AND relation, a new table JOIN is required for each clause. With very large meta tables, this can lead to poor performance.
Boone Georges

Boone Georges will replace the table joins in this case with sub queries. With WordPress 4.1 a more simple syntax will be written. Thanks for this!

Other Changes

In his blog post, he addresses also other changes, he want to include into WordPress 4.1.

So, for now I will have a look into these changes and see, if my search filter plugin is still running. If so, the next thing is to think, how my plugin can benefit from these new improvements 🙂

photo credit: Huasonic cc