Skip to content

Commit 3e0430d

Browse files
author
boonebgorges
committed
In WP_Meta_Query, don't cast meta_value to CHAR.
`CHAR` is redundant, since the `meta_value` column is `LONGTEXT`. Meanwhile, use of `CAST()` causes MySQL to ignore any index that the administrator may have added to the column. A number of automated tests were doing searches for `CAST` in the SQL strings generated by `WP_Meta_Query` (for reasons unrelated to the `CAST()` behavior). These tests have been updated to expect the new query format. Props ericlewis. Fixes #36625. git-svn-id: https://develop.svn.wordpress.org/trunk@37594 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d42d2c3 commit 3e0430d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/wp-includes/class-wp-meta-query.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,11 @@ public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' )
632632
}
633633

634634
if ( $where ) {
635-
$sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
635+
if ( 'CHAR' === $meta_type ) {
636+
$sql_chunks['where'][] = "$alias.meta_value {$meta_compare} {$where}";
637+
} else {
638+
$sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
639+
}
636640
}
637641
}
638642

tests/phpunit/tests/meta/query.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public function test_null_value_sql() {
493493
) );
494494
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
495495

496-
$this->assertEquals( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
496+
$this->assertEquals( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) );
497497
}
498498

499499
/**
@@ -601,7 +601,7 @@ public function test_convert_null_value_to_empty_string() {
601601

602602
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
603603

604-
$this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = ''" ) );
604+
$this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = ''" ) );
605605
}
606606

607607
public function test_get_sql_convert_lowercase_compare_to_uppercase() {
@@ -632,7 +632,7 @@ public function test_get_sql_empty_meta_compare_with_array_value() {
632632

633633
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
634634

635-
$this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) IN" ) );
635+
$this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value IN" ) );
636636
}
637637

638638
public function test_get_sql_empty_meta_compare_with_non_array_value() {
@@ -647,7 +647,7 @@ public function test_get_sql_empty_meta_compare_with_non_array_value() {
647647

648648
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
649649

650-
$this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) );
650+
$this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value =" ) );
651651
}
652652

653653
public function test_get_sql_invalid_meta_compare() {
@@ -663,7 +663,7 @@ public function test_get_sql_invalid_meta_compare() {
663663

664664
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
665665

666-
$this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) =" ) );
666+
$this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value =" ) );
667667
}
668668

669669
/**
@@ -760,7 +760,7 @@ public function test_get_sql_trim_string_value() {
760760

761761
$sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
762762

763-
$this->assertSame( 1, substr_count( $sql['where'], "CAST($wpdb->postmeta.meta_value AS CHAR) = 'bar'" ) );
763+
$this->assertSame( 1, substr_count( $sql['where'], "$wpdb->postmeta.meta_value = 'bar'" ) );
764764
}
765765

766766
public function test_not_exists() {

0 commit comments

Comments
 (0)