Introduce WP_Tax_Query. Fix canonical redirects in the process. See #15752
git-svn-id: http://svn.automattic.com/wordpress/trunk@16849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
3ed654c847
commit
e5690e7ebe
|
@ -146,7 +146,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
|
||||||
} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
|
} elseif ( is_category() || is_tag() || is_tax() ) { // Terms (Tags/categories)
|
||||||
|
|
||||||
$term_count = 0;
|
$term_count = 0;
|
||||||
foreach ( $wp_query->tax_query as $tax_query )
|
foreach ( $wp_query->tax_query->queries as $tax_query )
|
||||||
$term_count += count( $tax_query['terms'] );
|
$term_count += count( $tax_query['terms'] );
|
||||||
|
|
||||||
$obj = $wp_query->get_queried_object();
|
$obj = $wp_query->get_queried_object();
|
||||||
|
|
|
@ -714,9 +714,9 @@ class WP_Query {
|
||||||
*
|
*
|
||||||
* @since 3.1.0
|
* @since 3.1.0
|
||||||
* @access public
|
* @access public
|
||||||
* @var array
|
* @var object WP_Tax_Query
|
||||||
*/
|
*/
|
||||||
var $tax_query = array();
|
var $tax_query;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the data for a single object that is queried.
|
* Holds the data for a single object that is queried.
|
||||||
|
@ -1584,12 +1584,9 @@ class WP_Query {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_set_tax_query_defaults( $tax_query );
|
$tax_query_obj = new WP_Tax_Query( $tax_query );
|
||||||
|
|
||||||
foreach ( $tax_query as $query ) {
|
|
||||||
if ( ! is_array( $query ) )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
|
foreach ( $tax_query_obj->queries as $query ) {
|
||||||
if ( 'IN' == $query['operator'] ) {
|
if ( 'IN' == $query['operator'] ) {
|
||||||
switch ( $query['taxonomy'] ) {
|
switch ( $query['taxonomy'] ) {
|
||||||
case 'category':
|
case 'category':
|
||||||
|
@ -1604,7 +1601,7 @@ class WP_Query {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tax_query;
|
return $tax_query_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1942,7 +1939,7 @@ class WP_Query {
|
||||||
if ( $this->is_category || $this->is_tag || $this->is_tax ) {
|
if ( $this->is_category || $this->is_tag || $this->is_tax ) {
|
||||||
$this->tax_query = $this->parse_tax_query( $q );
|
$this->tax_query = $this->parse_tax_query( $q );
|
||||||
|
|
||||||
$clauses = call_user_func_array( 'get_tax_sql', array( $this->tax_query, $wpdb->posts, 'ID', &$this) );
|
$clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
|
||||||
|
|
||||||
$join .= $clauses['join'];
|
$join .= $clauses['join'];
|
||||||
$where .= $clauses['where'];
|
$where .= $clauses['where'];
|
||||||
|
@ -1957,7 +1954,7 @@ class WP_Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back-compat
|
// Back-compat
|
||||||
$tax_query_in = wp_list_filter( $this->tax_query, array( 'operator' => 'IN' ) );
|
$tax_query_in = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'IN' ) );
|
||||||
if ( !empty( $tax_query_in ) ) {
|
if ( !empty( $tax_query_in ) ) {
|
||||||
if ( !isset( $q['taxonomy'] ) ) {
|
if ( !isset( $q['taxonomy'] ) ) {
|
||||||
foreach ( $tax_query_in as $a_tax_query ) {
|
foreach ( $tax_query_in as $a_tax_query ) {
|
||||||
|
@ -2657,7 +2654,7 @@ class WP_Query {
|
||||||
$this->queried_object = NULL;
|
$this->queried_object = NULL;
|
||||||
$this->queried_object_id = 0;
|
$this->queried_object_id = 0;
|
||||||
|
|
||||||
$tax_query_in = wp_list_filter( $this->tax_query, array( 'operator' => 'IN' ) );
|
$tax_query_in = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'IN' ) );
|
||||||
if ( !empty( $tax_query_in ) ) {
|
if ( !empty( $tax_query_in ) ) {
|
||||||
$query = reset( $tax_query_in );
|
$query = reset( $tax_query_in );
|
||||||
|
|
||||||
|
|
|
@ -527,24 +527,49 @@ function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
|
$tax_query_obj = new WP_Tax_Query( $tax_query );
|
||||||
|
return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
|
||||||
|
}
|
||||||
|
|
||||||
|
class WP_Tax_Query {
|
||||||
|
var $relation = '';
|
||||||
|
var $queries = array();
|
||||||
|
|
||||||
|
function __construct( &$tax_query ) {
|
||||||
|
if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
|
||||||
|
$this->relation = 'OR';
|
||||||
|
} else {
|
||||||
|
$this->relation = 'AND';
|
||||||
|
}
|
||||||
|
|
||||||
|
$defaults = array(
|
||||||
|
'taxonomy' => '',
|
||||||
|
'terms' => array(),
|
||||||
|
'include_children' => true,
|
||||||
|
'field' => 'term_id',
|
||||||
|
'operator' => 'IN',
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach ( $tax_query as $query ) {
|
||||||
|
if ( ! is_array( $query ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$query = array_merge( $defaults, $query );
|
||||||
|
|
||||||
|
$query['terms'] = (array) $query['terms'];
|
||||||
|
|
||||||
|
$this->queries[] = $query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_sql( $primary_table, $primary_id_column ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$join = '';
|
$join = '';
|
||||||
$where = array();
|
$where = array();
|
||||||
$i = 0;
|
$i = 0;
|
||||||
|
|
||||||
_set_tax_query_defaults( $tax_query );
|
foreach ( $this->queries as $query ) {
|
||||||
|
|
||||||
if ( strtoupper( $tax_query['relation'] ) == 'OR' ) {
|
|
||||||
$relation = 'OR';
|
|
||||||
} else {
|
|
||||||
$relation = 'AND';
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ( $tax_query as $query ) {
|
|
||||||
if ( ! is_array( $query ) )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
extract( $query );
|
extract( $query );
|
||||||
|
|
||||||
if ( ! taxonomy_exists( $taxonomy ) )
|
if ( ! taxonomy_exists( $taxonomy ) )
|
||||||
|
@ -556,7 +581,7 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {
|
if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {
|
||||||
_transform_terms( $terms, $taxonomy, $field, 'term_id' );
|
$this->_transform_terms( $terms, $taxonomy, $field, 'term_id' );
|
||||||
|
|
||||||
$children = array();
|
$children = array();
|
||||||
foreach ( $terms as $term ) {
|
foreach ( $terms as $term ) {
|
||||||
|
@ -565,10 +590,10 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
}
|
}
|
||||||
$terms = $children;
|
$terms = $children;
|
||||||
|
|
||||||
_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );
|
$this->_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );
|
$this->_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'IN' == $operator ) {
|
if ( 'IN' == $operator ) {
|
||||||
|
@ -615,28 +640,6 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
|
||||||
return compact( 'join', 'where' );
|
return compact( 'join', 'where' );
|
||||||
}
|
}
|
||||||
|
|
||||||
function _set_tax_query_defaults( &$tax_query ) {
|
|
||||||
if ( ! isset( $tax_query['relation'] ) )
|
|
||||||
$tax_query['relation'] = 'AND';
|
|
||||||
|
|
||||||
$defaults = array(
|
|
||||||
'taxonomy' => '',
|
|
||||||
'terms' => array(),
|
|
||||||
'include_children' => true,
|
|
||||||
'field' => 'term_id',
|
|
||||||
'operator' => 'IN',
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ( $tax_query as $i => $query ) {
|
|
||||||
if ( ! is_array( $query ) )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$tax_query[$i] = array_merge( $defaults, $query );
|
|
||||||
|
|
||||||
$tax_query[$i]['terms'] = (array) $tax_query[$i]['terms'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
|
function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
|
@ -671,6 +674,7 @@ function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
|
||||||
" );
|
" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all Term data from database by Term ID.
|
* Get all Term data from database by Term ID.
|
||||||
|
|
Loading…
Reference in New Issue