Make WordPress Core

Changeset 37225


Ignore:
Timestamp:
04/17/2016 03:16:36 AM (8 years ago)
Author:
boonebgorges
Message:

Query: Allow results to be ordered by post_parent__in.

Props postpostmodern.
Fixes #36515.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r37075 r37225  
    14691469     *              Introduced the `$comment_status` and `$ping_status` parameters.
    14701470     *              Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
     1471     * @since 4.6.0 Added 'post_name__in' support for `$orderby`.
    14711472     * @access public
    14721473     *
     
    15241525     *                                                 'RAND(x)' (where 'x' is an integer seed value),
    15251526     *                                                 'comment_count', 'meta_value', 'meta_value_num', 'post__in',
    1526      *                                                 and the array keys of `$meta_query`.
     1527     *                                                 'post_name__in', 'post_parent__in', and the array keys
     1528     *                                                 of `$meta_query`.
    15271529     *     @type int          $p                       Post ID.
    15281530     *     @type int          $page                    Show the number of posts that would show up on page X of a
     
    27462748        } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
    27472749            $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
    2748             $where .= " AND $wpdb->posts.post_name IN ('" . implode( "' ,'", $q['post_name__in'] ) . "')";
     2750            $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
     2751            $where .= " AND $wpdb->posts.post_name IN ($post_name__in)";
    27492752        }
    27502753
     
    29642967        } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
    29652968            $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
     2969        } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
     2970            $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
    29662971        } else {
    29672972            $orderby_array = array();
  • trunk/tests/phpunit/tests/post/query.php

    r37224 r37225  
    179179    }
    180180
     181    /**
     182     * @ticket 36515
     183     */
     184    public function test_post_name__in_ordering() {
     185        $post_id1 = self::factory()->post->create( array( 'post_name' => 'id-1', 'post_type' => 'page' ) );
     186        $post_id2 = self::factory()->post->create( array( 'post_name' => 'id-2', 'post_type' => 'page' ) );
     187        $post_id3 = self::factory()->post->create( array(
     188            'post_name' => 'id-3',
     189            'post_type' => 'page',
     190            'post_parent' => $post_id2
     191        ) );
     192
     193        $ordered = array( 'id-2', 'id-3', 'id-1' );
     194
     195        $q = new WP_Query( array(
     196            'post_type' => 'any',
     197            'post_name__in' => $ordered,
     198            'orderby' => 'post_name__in'
     199        ) );
     200
     201        $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
     202    }
     203
    181204    function test_post_status() {
    182205        $statuses1 = get_post_stati();
Note: See TracChangeset for help on using the changeset viewer.