| 595 | | if ( 'LIKE' === $meta_compare_key ) { |
| 596 | | $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key LIKE %s", '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%' ); |
| 597 | | } else { |
| 598 | | $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); |
| | 614 | switch ( $meta_compare_key ) { |
| | 615 | case '=': |
| | 616 | $where = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); |
| | 617 | break; |
| | 618 | case 'LIKE': |
| | 619 | $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%'; |
| | 620 | $where = $wpdb->prepare( "$alias.meta_key LIKE %s", $meta_compare_value ); |
| | 621 | break; |
| | 622 | case 'IN': |
| | 623 | $operator = $meta_compare_key; |
| | 624 | $meta_compare_string = "$alias.meta_key $operator (" . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ')'; |
| | 625 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); |
| | 626 | break; |
| | 627 | |
| | 628 | /** |
| | 629 | * In joined clauses negative operators have to be nested |
| | 630 | * into a NOT EXISTS clause and flipped. Otherwise, non-matching results |
| | 631 | * containing the post_IDs will be returned, rendering the |
| | 632 | * operator ineffective. |
| | 633 | */ |
| | 634 | case '!=': |
| | 635 | $i = count( $this->table_aliases ); |
| | 636 | $subquery_alias = $i ? 'mt' . $i : $this->meta_table; |
| | 637 | $this->table_aliases[] = $subquery_alias; |
| | 638 | |
| | 639 | $meta_compare_string = "NOT EXISTS ("; |
| | 640 | $meta_compare_string .= "SELECT 1 FROM $wpdb->postmeta $subquery_alias "; |
| | 641 | $meta_compare_string .= "WHERE $subquery_alias.post_ID = $alias.post_ID "; |
| | 642 | $meta_compare_string .= "AND $subquery_alias.meta_key = %s "; |
| | 643 | $meta_compare_string .= 'LIMIT 1'; |
| | 644 | $meta_compare_string .= ")"; |
| | 645 | |
| | 646 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); |
| | 647 | break; |
| | 648 | case 'NOT LIKE': |
| | 649 | $i = count( $this->table_aliases ); |
| | 650 | $subquery_alias = $i ? 'mt' . $i : $this->meta_table; |
| | 651 | $this->table_aliases[] = $subquery_alias; |
| | 652 | |
| | 653 | $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%'; |
| | 654 | |
| | 655 | $meta_compare_string = "NOT EXISTS ("; |
| | 656 | $meta_compare_string .= "SELECT 1 FROM $wpdb->postmeta $subquery_alias "; |
| | 657 | $meta_compare_string .= "WHERE $subquery_alias.post_ID = $alias.post_ID "; |
| | 658 | $meta_compare_string .= "AND $subquery_alias.meta_key LIKE %s "; |
| | 659 | $meta_compare_string .= 'LIMIT 1'; |
| | 660 | $meta_compare_string .= ")"; |
| | 661 | |
| | 662 | $where = $wpdb->prepare( $meta_compare_string, $meta_compare_value ); |
| | 663 | break; |
| | 664 | case 'NOT IN': |
| | 665 | $i = count( $this->table_aliases ); |
| | 666 | $subquery_alias = $i ? 'mt' . $i : $this->meta_table; |
| | 667 | $this->table_aliases[] = $subquery_alias; |
| | 668 | |
| | 669 | $meta_compare_string = "NOT EXISTS ("; |
| | 670 | $meta_compare_string .= "SELECT 1 FROM $wpdb->postmeta $subquery_alias "; |
| | 671 | $meta_compare_string .= "WHERE $subquery_alias.post_ID = $alias.post_ID "; |
| | 672 | $meta_compare_string .= "AND $subquery_alias.meta_key IN "; |
| | 673 | $meta_compare_string .= '(' . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ') '; |
| | 674 | $meta_compare_string .= 'LIMIT 1'; |
| | 675 | $meta_compare_string .= ")"; |
| | 676 | |
| | 677 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); |
| | 678 | break; |