WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/08/2019 03:32:04 AM (3 years ago)
Author:
boonebgorges
Message:

Query: Standardize treatment of 'orderby' values post__in, post_parent__in, and post_name__in.

Ordering by post__in was introduced in [21776], but the code assumed that
post__in would be a comma-separated string listing post IDs. When an array
of post IDs was passed to the post__in query var, 'orderby=postin' was
not respected. This changeset changes this behavior by handling
'orderby=post
in' in the same way as most other values of 'orderby',
which ensures that arrays as well as strings can be properly parsed.

The same treatment is given to the similar post_name__in and
post_parent__in options of 'orderby', so that most query generation for
orderby clauses happens in the same place, instead of in special cases.

A slight change in the resulting SQL (related to the whitespace around
parentheses and commas) necessitates a change to an existing REST API test
that does a string comparison against the SQL query.

Props mgibbs189, kelvink.
Fixes #38034.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/query.php

    r43571 r44452  
    172172            )
    173173        );
     174        $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
     175    }
     176
     177    /**
     178     * @ticket 38034
     179     */
     180    public function test_orderby_post__in_array() {
     181        $posts = self::factory()->post->create_many( 4 );
     182
     183        $ordered = array( $posts[2], $posts[0], $posts[3] );
     184
     185        $q = new WP_Query( array(
     186            'post_type' => 'any',
     187            'post__in' => $ordered,
     188            'orderby' => array( 'post__in' => 'ASC' ),
     189        ) );
     190        $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
     191    }
     192
     193    /**
     194     * @ticket 38034
     195     */
     196    public function test_orderby_post__in_array_with_implied_order() {
     197        $posts = self::factory()->post->create_many( 4 );
     198
     199        $ordered = array( $posts[2], $posts[0], $posts[3] );
     200
     201        $q = new WP_Query( array(
     202            'post_type' => 'any',
     203            'post__in' => $ordered,
     204            'orderby' => 'post__in',
     205        ) );
    174206        $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) );
    175207    }
Note: See TracChangeset for help on using the changeset viewer.