Query: Allow results to be ordered by post_parent__in.

Props postpostmodern.
Fixes #36515.

git-svn-id: https://develop.svn.wordpress.org/trunk@37225 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-04-17 03:16:36 +00:00
parent d73ec171c0
commit 8ee26e699f
2 changed files with 30 additions and 2 deletions

View File

@ -1468,6 +1468,7 @@ class WP_Query {
* @since 4.5.0 Removed the `$comments_popup` parameter.
* Introduced the `$comment_status` and `$ping_status` parameters.
* Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
* @since 4.6.0 Added 'post_name__in' support for `$orderby`.
* @access public
*
* @param string|array $query {
@ -1523,7 +1524,8 @@ class WP_Query {
* 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
* 'RAND(x)' (where 'x' is an integer seed value),
* 'comment_count', 'meta_value', 'meta_value_num', 'post__in',
* and the array keys of `$meta_query`.
* 'post_name__in', 'post_parent__in', and the array keys
* of `$meta_query`.
* @type int $p Post ID.
* @type int $page Show the number of posts that would show up on page X of a
* static front page.
@ -2745,7 +2747,8 @@ class WP_Query {
$where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
} elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
$q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
$where .= " AND $wpdb->posts.post_name IN ('" . implode( "' ,'", $q['post_name__in'] ) . "')";
$post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
$where .= " AND $wpdb->posts.post_name IN ($post_name__in)";
}
// If an attachment is requested by number, let it supersede any post number.
@ -2963,6 +2966,8 @@ class WP_Query {
$orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
} elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
$orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
} elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
$orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
} else {
$orderby_array = array();
if ( is_array( $q['orderby'] ) ) {

View File

@ -178,6 +178,29 @@ class Tests_Post_Query extends WP_UnitTestCase {
$this->assertEqualSets( $ordered, wp_list_pluck( $attached->posts, 'ID' ) );
}
/**
* @ticket 36515
*/
public function test_post_name__in_ordering() {
$post_id1 = self::factory()->post->create( array( 'post_name' => 'id-1', 'post_type' => 'page' ) );
$post_id2 = self::factory()->post->create( array( 'post_name' => 'id-2', 'post_type' => 'page' ) );
$post_id3 = self::factory()->post->create( array(
'post_name' => 'id-3',
'post_type' => 'page',
'post_parent' => $post_id2
) );
$ordered = array( 'id-2', 'id-3', 'id-1' );
$q = new WP_Query( array(
'post_type' => 'any',
'post_name__in' => $ordered,
'orderby' => 'post_name__in'
) );
$this->assertSame( $ordered, wp_list_pluck( $q->posts, 'post_name' ) );
}
function test_post_status() {
$statuses1 = get_post_stati();
$this->assertContains( 'auto-draft', $statuses1 );