equal
deleted
inserted
replaced
94 * @param array $tax_query { |
94 * @param array $tax_query { |
95 * Array of taxonomy query clauses. |
95 * Array of taxonomy query clauses. |
96 * |
96 * |
97 * @type string $relation Optional. The MySQL keyword used to join |
97 * @type string $relation Optional. The MySQL keyword used to join |
98 * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. |
98 * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. |
99 * @type array { |
99 * @type array ...$0 { |
100 * Optional. An array of first-order clause parameters, or another fully-formed tax query. |
100 * An array of first-order clause parameters, or another fully-formed tax query. |
101 * |
101 * |
102 * @type string $taxonomy Taxonomy being queried. Optional when field=term_taxonomy_id. |
102 * @type string $taxonomy Taxonomy being queried. Optional when field=term_taxonomy_id. |
103 * @type string|int|array $terms Term or terms to filter by. |
103 * @type string|int|array $terms Term or terms to filter by. |
104 * @type string $field Field to match $terms against. Accepts 'term_id', 'slug', |
104 * @type string $field Field to match $terms against. Accepts 'term_id', 'slug', |
105 * 'name', or 'term_taxonomy_id'. Default: 'term_id'. |
105 * 'name', or 'term_taxonomy_id'. Default: 'term_id'. |
386 $sql = array( |
386 $sql = array( |
387 'where' => array(), |
387 'where' => array(), |
388 'join' => array(), |
388 'join' => array(), |
389 ); |
389 ); |
390 |
390 |
391 $join = $where = ''; |
391 $join = ''; |
|
392 $where = ''; |
392 |
393 |
393 $this->clean_query( $clause ); |
394 $this->clean_query( $clause ); |
394 |
395 |
395 if ( is_wp_error( $clause ) ) { |
396 if ( is_wp_error( $clause ) ) { |
396 return self::$no_results; |
397 return self::$no_results; |
397 } |
398 } |
398 |
399 |
399 $terms = $clause['terms']; |
400 $terms = $clause['terms']; |
400 $operator = strtoupper( $clause['operator'] ); |
401 $operator = strtoupper( $clause['operator'] ); |
401 |
402 |
402 if ( 'IN' == $operator ) { |
403 if ( 'IN' === $operator ) { |
403 |
404 |
404 if ( empty( $terms ) ) { |
405 if ( empty( $terms ) ) { |
405 return self::$no_results; |
406 return self::$no_results; |
406 } |
407 } |
407 |
408 |
427 $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)"; |
428 $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)"; |
428 } |
429 } |
429 |
430 |
430 $where = "$alias.term_taxonomy_id $operator ($terms)"; |
431 $where = "$alias.term_taxonomy_id $operator ($terms)"; |
431 |
432 |
432 } elseif ( 'NOT IN' == $operator ) { |
433 } elseif ( 'NOT IN' === $operator ) { |
433 |
434 |
434 if ( empty( $terms ) ) { |
435 if ( empty( $terms ) ) { |
435 return $sql; |
436 return $sql; |
436 } |
437 } |
437 |
438 |
441 SELECT object_id |
442 SELECT object_id |
442 FROM $wpdb->term_relationships |
443 FROM $wpdb->term_relationships |
443 WHERE term_taxonomy_id IN ($terms) |
444 WHERE term_taxonomy_id IN ($terms) |
444 )"; |
445 )"; |
445 |
446 |
446 } elseif ( 'AND' == $operator ) { |
447 } elseif ( 'AND' === $operator ) { |
447 |
448 |
448 if ( empty( $terms ) ) { |
449 if ( empty( $terms ) ) { |
449 return $sql; |
450 return $sql; |
450 } |
451 } |
451 |
452 |
494 * join. In the case of WP_Tax_Query, this only applies to 'IN' |
495 * join. In the case of WP_Tax_Query, this only applies to 'IN' |
495 * clauses that are connected by the relation 'OR'. |
496 * clauses that are connected by the relation 'OR'. |
496 * |
497 * |
497 * @since 4.1.0 |
498 * @since 4.1.0 |
498 * |
499 * |
499 * @param array $clause Query clause. |
500 * @param array $clause Query clause. |
500 * @param array $parent_query Parent query of $clause. |
501 * @param array $parent_query Parent query of $clause. |
501 * @return string|false Table alias if found, otherwise false. |
502 * @return string|false Table alias if found, otherwise false. |
502 */ |
503 */ |
503 protected function find_compatible_table_alias( $clause, $parent_query ) { |
504 protected function find_compatible_table_alias( $clause, $parent_query ) { |
504 $alias = false; |
505 $alias = false; |
505 |
506 |
506 // Sanity check. Only IN queries use the JOIN syntax . |
507 // Sanity check. Only IN queries use the JOIN syntax. |
507 if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) { |
508 if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) { |
508 return $alias; |
509 return $alias; |
509 } |
510 } |
510 |
511 |
511 // Since we're only checking IN queries, we're only concerned with OR relations. |
512 // Since we're only checking IN queries, we're only concerned with OR relations. |
523 if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) { |
524 if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) { |
524 continue; |
525 continue; |
525 } |
526 } |
526 |
527 |
527 // The sibling must both have compatible operator to share its alias. |
528 // The sibling must both have compatible operator to share its alias. |
528 if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators ) ) { |
529 if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators, true ) ) { |
529 $alias = $sibling['alias']; |
530 $alias = $sibling['alias']; |
530 break; |
531 break; |
531 } |
532 } |
532 } |
533 } |
533 |
534 |
546 if ( 'term_taxonomy_id' !== $query['field'] ) { |
547 if ( 'term_taxonomy_id' !== $query['field'] ) { |
547 $query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
548 $query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
548 return; |
549 return; |
549 } |
550 } |
550 |
551 |
551 // so long as there are shared terms, include_children requires that a taxonomy is set |
552 // So long as there are shared terms, 'include_children' requires that a taxonomy is set. |
552 $query['include_children'] = false; |
553 $query['include_children'] = false; |
553 } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { |
554 } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { |
554 $query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
555 $query = new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); |
555 return; |
556 return; |
556 } |
557 } |
638 if ( is_wp_error( $term_list ) ) { |
639 if ( is_wp_error( $term_list ) ) { |
639 $query = $term_list; |
640 $query = $term_list; |
640 return; |
641 return; |
641 } |
642 } |
642 |
643 |
643 if ( 'AND' == $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) { |
644 if ( 'AND' === $query['operator'] && count( $term_list ) < count( $query['terms'] ) ) { |
644 $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) ); |
645 $query = new WP_Error( 'inexistent_terms', __( 'Inexistent terms.' ) ); |
645 return; |
646 return; |
646 } |
647 } |
647 |
648 |
648 $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); |
649 $query['terms'] = wp_list_pluck( $term_list, $resulting_field ); |