From 926427e9ee1337c69f43bb557c50a544568010ce Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 21 Nov 2013 19:30:35 +0000 Subject: [PATCH] In `update_meta_cache()`, ensure that meta is always stored in the same order. Removes an unnecessary `$wpdb->prepare` statement. Adds unit test. Props mattheu. Fixes #25511. git-svn-id: https://develop.svn.wordpress.org/trunk@26307 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/meta.php | 6 +++--- tests/phpunit/tests/meta.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 82cb49144a..55beadb1a4 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -558,9 +558,9 @@ function update_meta_cache($meta_type, $object_ids) { return $cache; // Get meta info - $id_list = join(',', $ids); - $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)", - $meta_type), ARRAY_A ); + $id_list = join( ',', $ids ); + $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; + $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); if ( !empty($meta_list) ) { foreach ( $meta_list as $metarow) { diff --git a/tests/phpunit/tests/meta.php b/tests/phpunit/tests/meta.php index 2bf5ac3944..d9e0e903dd 100644 --- a/tests/phpunit/tests/meta.php +++ b/tests/phpunit/tests/meta.php @@ -171,4 +171,19 @@ class Tests_Meta extends WP_UnitTestCase { $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts ); $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) ); } + + function test_meta_cache_order_asc() { + $post_id = $this->factory->post->create(); + $colors = array( 'red', 'blue', 'yellow', 'green' ); + foreach ( $colors as $color ) + add_post_meta( $post_id, 'color', $color ); + + foreach ( range( 1, 10 ) as $i ) { + $meta = get_post_meta( $post_id, 'color' ); + $this->assertEquals( $meta, $colors ); + + if ( 0 === $i % 2 ) + wp_cache_delete( $post_id, 'post_meta' ); + } + } }