Introduce WP_Tax_Query. Fix canonical redirects in the process. See #15752
git-svn-id: https://develop.svn.wordpress.org/trunk@16849 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
a239d692a7
commit
85a176fe8f
|
@ -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,148 +527,152 @@ 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 ) {
|
||||||
global $wpdb;
|
$tax_query_obj = new WP_Tax_Query( $tax_query );
|
||||||
|
return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
|
||||||
|
}
|
||||||
|
|
||||||
$join = '';
|
class WP_Tax_Query {
|
||||||
$where = array();
|
var $relation = '';
|
||||||
$i = 0;
|
var $queries = array();
|
||||||
|
|
||||||
_set_tax_query_defaults( $tax_query );
|
function __construct( &$tax_query ) {
|
||||||
|
if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
|
||||||
|
$this->relation = 'OR';
|
||||||
|
} else {
|
||||||
|
$this->relation = 'AND';
|
||||||
|
}
|
||||||
|
|
||||||
if ( strtoupper( $tax_query['relation'] ) == 'OR' ) {
|
$defaults = array(
|
||||||
$relation = 'OR';
|
'taxonomy' => '',
|
||||||
} else {
|
'terms' => array(),
|
||||||
$relation = 'AND';
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $tax_query as $query ) {
|
function get_sql( $primary_table, $primary_id_column ) {
|
||||||
if ( ! is_array( $query ) )
|
global $wpdb;
|
||||||
continue;
|
|
||||||
|
|
||||||
extract( $query );
|
$join = '';
|
||||||
|
$where = array();
|
||||||
|
$i = 0;
|
||||||
|
|
||||||
if ( ! taxonomy_exists( $taxonomy ) )
|
foreach ( $this->queries as $query ) {
|
||||||
return array( 'join' => '', 'where' => ' AND 0 = 1');
|
extract( $query );
|
||||||
|
|
||||||
$terms = array_unique( (array) $terms );
|
if ( ! taxonomy_exists( $taxonomy ) )
|
||||||
|
return array( 'join' => '', 'where' => ' AND 0 = 1');
|
||||||
|
|
||||||
if ( empty( $terms ) )
|
$terms = array_unique( (array) $terms );
|
||||||
continue;
|
|
||||||
|
|
||||||
if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {
|
|
||||||
_transform_terms( $terms, $taxonomy, $field, 'term_id' );
|
|
||||||
|
|
||||||
$children = array();
|
|
||||||
foreach ( $terms as $term ) {
|
|
||||||
$children = array_merge( $children, get_term_children( $term, $taxonomy ) );
|
|
||||||
$children[] = $term;
|
|
||||||
}
|
|
||||||
$terms = $children;
|
|
||||||
|
|
||||||
_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( 'IN' == $operator ) {
|
|
||||||
|
|
||||||
if ( empty( $terms ) ) {
|
|
||||||
if ( 'OR' == $relation )
|
|
||||||
continue;
|
|
||||||
else
|
|
||||||
return array( 'join' => '', 'where' => ' AND 0 = 1' );
|
|
||||||
}
|
|
||||||
|
|
||||||
$terms = implode( ',', $terms );
|
|
||||||
|
|
||||||
$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
|
|
||||||
|
|
||||||
$join .= " INNER JOIN $wpdb->term_relationships";
|
|
||||||
$join .= $i ? " AS $alias" : '';
|
|
||||||
$join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
|
|
||||||
|
|
||||||
$where[] = "$alias.term_taxonomy_id $operator ($terms)";
|
|
||||||
}
|
|
||||||
elseif ( 'NOT IN' == $operator ) {
|
|
||||||
|
|
||||||
if ( empty( $terms ) )
|
if ( empty( $terms ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
$terms = implode( ',', $terms );
|
if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {
|
||||||
|
$this->_transform_terms( $terms, $taxonomy, $field, 'term_id' );
|
||||||
|
|
||||||
$where[] = "$primary_table.$primary_id_column NOT IN (
|
$children = array();
|
||||||
SELECT object_id
|
foreach ( $terms as $term ) {
|
||||||
FROM $wpdb->term_relationships
|
$children = array_merge( $children, get_term_children( $term, $taxonomy ) );
|
||||||
WHERE term_taxonomy_id IN ($terms)
|
$children[] = $term;
|
||||||
)";
|
}
|
||||||
|
$terms = $children;
|
||||||
|
|
||||||
|
$this->_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$this->_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 'IN' == $operator ) {
|
||||||
|
|
||||||
|
if ( empty( $terms ) ) {
|
||||||
|
if ( 'OR' == $relation )
|
||||||
|
continue;
|
||||||
|
else
|
||||||
|
return array( 'join' => '', 'where' => ' AND 0 = 1' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$terms = implode( ',', $terms );
|
||||||
|
|
||||||
|
$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
|
||||||
|
|
||||||
|
$join .= " INNER JOIN $wpdb->term_relationships";
|
||||||
|
$join .= $i ? " AS $alias" : '';
|
||||||
|
$join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
|
||||||
|
|
||||||
|
$where[] = "$alias.term_taxonomy_id $operator ($terms)";
|
||||||
|
}
|
||||||
|
elseif ( 'NOT IN' == $operator ) {
|
||||||
|
|
||||||
|
if ( empty( $terms ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$terms = implode( ',', $terms );
|
||||||
|
|
||||||
|
$where[] = "$primary_table.$primary_id_column NOT IN (
|
||||||
|
SELECT object_id
|
||||||
|
FROM $wpdb->term_relationships
|
||||||
|
WHERE term_taxonomy_id IN ($terms)
|
||||||
|
)";
|
||||||
|
}
|
||||||
|
|
||||||
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$i++;
|
if ( !empty( $where ) )
|
||||||
|
$where = ' AND ( ' . implode( " $relation ", $where ) . ' )';
|
||||||
|
else
|
||||||
|
$where = '';
|
||||||
|
|
||||||
|
return compact( 'join', 'where' );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !empty( $where ) )
|
function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
|
||||||
$where = ' AND ( ' . implode( " $relation ", $where ) . ' )';
|
global $wpdb;
|
||||||
else
|
|
||||||
$where = '';
|
|
||||||
|
|
||||||
return compact( 'join', 'where' );
|
if ( empty( $terms ) )
|
||||||
}
|
return;
|
||||||
|
|
||||||
function _set_tax_query_defaults( &$tax_query ) {
|
if ( $field == $resulting_field )
|
||||||
if ( ! isset( $tax_query['relation'] ) )
|
return;
|
||||||
$tax_query['relation'] = 'AND';
|
|
||||||
|
|
||||||
$defaults = array(
|
$resulting_field = esc_sql( $resulting_field );
|
||||||
'taxonomy' => '',
|
|
||||||
'terms' => array(),
|
|
||||||
'include_children' => true,
|
|
||||||
'field' => 'term_id',
|
|
||||||
'operator' => 'IN',
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ( $tax_query as $i => $query ) {
|
switch ( $field ) {
|
||||||
if ( ! is_array( $query ) )
|
case 'slug':
|
||||||
continue;
|
case 'name':
|
||||||
|
$terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'";
|
||||||
|
$terms = $wpdb->get_col( "
|
||||||
|
SELECT $wpdb->term_taxonomy.$resulting_field
|
||||||
|
FROM $wpdb->term_taxonomy
|
||||||
|
INNER JOIN $wpdb->terms USING (term_id)
|
||||||
|
WHERE taxonomy = '$taxonomy'
|
||||||
|
AND $wpdb->terms.$field IN ($terms)
|
||||||
|
" );
|
||||||
|
break;
|
||||||
|
|
||||||
$tax_query[$i] = array_merge( $defaults, $query );
|
default:
|
||||||
|
$terms = implode( ',', array_map( 'intval', $terms ) );
|
||||||
$tax_query[$i]['terms'] = (array) $tax_query[$i]['terms'];
|
$terms = $wpdb->get_col( "
|
||||||
}
|
SELECT $resulting_field
|
||||||
}
|
FROM $wpdb->term_taxonomy
|
||||||
|
WHERE taxonomy = '$taxonomy'
|
||||||
function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
|
AND term_id IN ($terms)
|
||||||
global $wpdb;
|
" );
|
||||||
|
}
|
||||||
if ( empty( $terms ) )
|
|
||||||
return;
|
|
||||||
|
|
||||||
if ( $field == $resulting_field )
|
|
||||||
return;
|
|
||||||
|
|
||||||
$resulting_field = esc_sql( $resulting_field );
|
|
||||||
|
|
||||||
switch ( $field ) {
|
|
||||||
case 'slug':
|
|
||||||
case 'name':
|
|
||||||
$terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'";
|
|
||||||
$terms = $wpdb->get_col( "
|
|
||||||
SELECT $wpdb->term_taxonomy.$resulting_field
|
|
||||||
FROM $wpdb->term_taxonomy
|
|
||||||
INNER JOIN $wpdb->terms USING (term_id)
|
|
||||||
WHERE taxonomy = '$taxonomy'
|
|
||||||
AND $wpdb->terms.$field IN ($terms)
|
|
||||||
" );
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
$terms = implode( ',', array_map( 'intval', $terms ) );
|
|
||||||
$terms = $wpdb->get_col( "
|
|
||||||
SELECT $resulting_field
|
|
||||||
FROM $wpdb->term_taxonomy
|
|
||||||
WHERE taxonomy = '$taxonomy'
|
|
||||||
AND term_id IN ($terms)
|
|
||||||
" );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue