Make WordPress Core

Changeset 31321


Ignore:
Timestamp:
02/01/2015 08:25:42 PM (10 years ago)
Author:
boonebgorges
Message:

When querying for a specific post, allow posts with a non-public status to be returned as long as that status is specified.

This makes it possible to, for example, retrieve a specific post using the
p parameter of WP_Query, even if the post is in the Trash, by including
the post_status=trash parameter.

Props ebinnion.
Fixes #29167.

Location:
trunk
Files:
2 edited

Legend:

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

    r31312 r31321  
    29642964        $user_id = get_current_user_id();
    29652965
     2966        $q_status = array();
    29662967        if ( ! empty( $q['post_status'] ) ) {
    29672968            $statuswheres = array();
     
    35283529            $post_status_obj = get_post_status_object($status);
    35293530            //$type = get_post_type($this->posts[0]);
    3530             if ( !$post_status_obj->public ) {
     3531
     3532            // If the post_status was specifically requested, let it pass through.
     3533            if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) {
     3534
    35313535                if ( ! is_user_logged_in() ) {
    35323536                    // User must be logged in to view unpublished posts.
  • trunk/tests/phpunit/tests/query/postStatus.php

    r31114 r31321  
    299299        $this->assertEmpty( $q->posts );
    300300    }
     301
     302    /**
     303     * @ticket 29167
     304     */
     305    public function test_specific_post_should_be_returned_if_trash_is_one_of_the_requested_post_statuses() {
     306        $p1 = $this->factory->post->create( array( 'post_status' => 'trash' ) );
     307        $p2 = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     308
     309        $q = new WP_Query( array(
     310            'p' => $p1,
     311            'post_status' => array( 'trash', 'publish' ),
     312        ) );
     313
     314        $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) );
     315    }
    301316}
Note: See TracChangeset for help on using the changeset viewer.