[wp-trac] [WordPress Trac] #28568: Can't get private posts/pages via WP_Query when not logged in as administrator

WordPress Trac noreply at wordpress.org
Wed Jun 6 09:55:19 UTC 2018


#28568: Can't get private posts/pages via WP_Query when not logged in as
administrator
--------------------------+-----------------------
 Reporter:  mklusak       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:
Component:  Query         |     Version:  3.9
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+-----------------------
Changes (by dnaber-de):

 * status:  closed => reopened
 * resolution:  invalid =>


Comment:

 For everyone who stumble over this bug there is a solution to bypass:

 {{{#!php
 <?php
 $posts = [];
 $query = new \WP_Query();
 $fetchPosts = function ( array $postResults, \WP_Query $currentQuery ) use
 ( &$posts, $query ): array {

         if ( $currentQuery === $query ) {
                 $posts = $postResults;
         }
         return $postResults;
 };
 add_filter( 'posts_results', $fetchPosts, 10, 2 );
 $query->query( ['post_status' => 'any'] );
 remove_filter( 'posts_results', $fetchPosts, 10 );
 }}}

 @ocean90 Maybe you want to rethink your decision. I know of no reason why
 WP_Query (which is the only API to fetch posts from database) should
 depend on global state whether a user is logged in or not. That makes no
 sense. These security «protections» inside WP_Query::get_posts() should at
 least only act if it's the main query or should be bypassed by the
 suppress_filters parameter.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/28568#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list