<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[49830] trunk: Query: Respect post-type specific capabilities when querying for multiple post types.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/49830">49830</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/49830","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-12-17 16:15:38 +0000 (Thu, 17 Dec 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Query: Respect post-type specific capabilities when querying for multiple post types.

After this change, the relevant `read_private_posts` capability is checked for
each queried post type. This ensures that private posts appear in search and
archive queries for users who have the ability to view those posts.

Props leogermani.

Fixes <a href="https://core.trac.wordpress.org/ticket/13509">#13509</a>, <a href="https://core.trac.wordpress.org/ticket/48968">#48968</a>, <a href="https://core.trac.wordpress.org/ticket/48556">#48556</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunktestsphpunittestsquerypostStatusphp">trunk/tests/phpunit/tests/query/postStatus.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2020-12-17 15:08:06 UTC (rev 49829)
+++ trunk/src/wp-includes/class-wp-query.php    2020-12-17 16:15:38 UTC (rev 49830)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2420,26 +2420,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $has_valid_post_types = true;
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'any' === $post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( empty( $in_search_post_types ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where .= ' AND 1=0 ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $post_type_where      = ' AND 1=0 ';
+                               $has_valid_post_types = true;
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( ! empty( $post_type ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( $post_type );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( $this->is_attachment ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'attachment'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'attachment'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'attachment' );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( $this->is_page ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'page'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'page'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'page' );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'post'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'post'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'post' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2457,7 +2459,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $user_id = get_current_user_id();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $q_status = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $q['post_status'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( ! $has_valid_post_types ) {
+                       // When there are no public post types, there's no need to assemble the post_status clause.
+                       $where .= $post_type_where;
+               } elseif ( ! empty( $q['post_status'] ) ) {
+                       $where .= $post_type_where;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $statuswheres = array();
</span><span class="cx" style="display: block; padding: 0 10px">                        $q_status     = $q['post_status'];
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! is_array( $q_status ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2516,40 +2524,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $where_status ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $where .= " AND ($where_status)";
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( ! $this->is_singular ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= " AND ({$wpdb->posts}.post_status = 'publish'";
-
-                       // Add public states.
-                       $public_states = get_post_stati( array( 'public' => true ) );
-                       foreach ( (array) $public_states as $state ) {
-                               if ( 'publish' === $state ) { // Publish is hard-coded above.
-                                       continue;
-                               }
-                               $where .= " OR {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( 'any' === $post_type ) {
+                               $queried_post_types = get_post_types( array( 'exclude_from_search' => false ) );
+                       } elseif ( is_array( $post_type ) ) {
+                               $queried_post_types = $post_type;
+                       } elseif ( ! empty( $post_type ) ) {
+                               $queried_post_types = array( $post_type );
+                       } else {
+                               $queried_post_types = array( 'post' );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( $this->is_admin ) {
-                               // Add protected states that should show in the admin all list.
-                               $admin_all_states = get_post_stati(
-                                       array(
-                                               'protected'              => true,
-                                               'show_in_admin_all_list' => true,
-                                       )
-                               );
-                               foreach ( (array) $admin_all_states as $state ) {
-                                       $where .= " OR {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! empty( $queried_post_types ) ) {
+                               $status_type_clauses = array();
+
+                               // Assemble a post_status clause for each post type.
+                               foreach ( $queried_post_types as $queried_post_type ) {
+                                       $queried_post_type_object = get_post_type_object( $queried_post_type );
+                                       if ( ! $queried_post_type_object instanceof \WP_Post_Type ) {
+                                               continue;
+                                       }
+
+                                       $type_where = '(' . $wpdb->prepare( "{$wpdb->posts}.post_type = %s AND (", $queried_post_type );
+
+                                       // Public statuses.
+                                       $public_statuses = get_post_stati( array( 'public' => true ) );
+                                       $status_clauses  = [];
+                                       foreach ( (array) $public_statuses as $public_status ) {
+                                               $status_clauses[] = "{$wpdb->posts}.post_status = '$public_status'";
+                                       }
+                                       $type_where .= implode( ' OR ', $status_clauses );
+
+                                       // Add protected states that should show in the admin all list.
+                                       if ( $this->is_admin ) {
+                                               $admin_all_statuses = get_post_stati(
+                                                       array(
+                                                               'protected'              => true,
+                                                               'show_in_admin_all_list' => true,
+                                                       )
+                                               );
+                                               foreach ( (array) $admin_all_statuses as $admin_all_status ) {
+                                                       $type_where .= " OR {$wpdb->posts}.post_status = '$admin_all_status'";
+                                               }
+                                       }
+
+                                       // Add private states that are visible to current user.
+                                       if ( is_user_logged_in() ) {
+                                               $read_private_cap = $queried_post_type_object->cap->read_private_posts;
+                                               $private_statuses = get_post_stati( array( 'private' => true ) );
+                                               foreach ( (array) $private_statuses as $private_status ) {
+                                                       $type_where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$private_status'" : " OR ({$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$private_status')";
+                                               }
+                                       }
+
+                                       $type_where .= '))';
+
+                                       $status_type_clauses[] = $type_where;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( is_user_logged_in() ) {
-                               // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
-                               $private_states = get_post_stati( array( 'private' => true ) );
-                               foreach ( (array) $private_states as $state ) {
-                                       $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! empty( $status_type_clauses ) ) {
+                                       $where .= ' AND (' . implode( ' OR ', $status_type_clauses ) . ')';
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        } else {
+                               $where .= ' AND 1=0 ';
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= ')';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } else {
+                       $where .= $post_type_where;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /*
</span></span></pre></div>
<a id="trunktestsphpunittestsquerypostStatusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/postStatus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/postStatus.php    2020-12-17 15:08:06 UTC (rev 49829)
+++ trunk/tests/phpunit/tests/query/postStatus.php      2020-12-17 16:15:38 UTC (rev 49830)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,6 +6,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Query_PostStatus extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">        public static $editor_user_id;
</span><span class="cx" style="display: block; padding: 0 10px">        public static $author_user_id;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public static $subscriber_user_id;
</ins><span class="cx" style="display: block; padding: 0 10px">         public static $editor_private_post;
</span><span class="cx" style="display: block; padding: 0 10px">        public static $author_private_post;
</span><span class="cx" style="display: block; padding: 0 10px">        public static $editor_privatefoo_post;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,8 +13,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public static $author_privatefoo_post;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) );
-               self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$editor_user_id     = $factory->user->create( array( 'role' => 'editor' ) );
+               self::$author_user_id     = $factory->user->create( array( 'role' => 'author' ) );
+               self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                self::$editor_private_post = $factory->post->create(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -457,4 +459,162 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 48556
+        * @ticket 13509
+        */
+       public function test_non_singular_queries_using_post_type_any_should_respect_post_type_read_private_posts_cap() {
+               register_post_type(
+                       'wptests_pt1',
+                       array(
+                               'exclude_from_search' => false,
+                               'capabilities'        => [
+                                       'read_private_posts' => 'read_private_pt1s',
+                               ],
+                       )
+               );
+
+               register_post_type(
+                       'wptests_pt2',
+                       array(
+                               'exclude_from_search' => false,
+                       )
+               );
+
+               $post_ids = array();
+
+               $post_ids['wptests_pt1_p1'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt1_p2'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt2_p1'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt2_p2'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               wp_set_current_user( 0 );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => 'any',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+
+               wp_set_current_user( self::$subscriber_user_id );
+               $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => 'any',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+       }
+
+       /**
+        * @ticket 48556
+        * @ticket 13509
+        */
+       public function test_non_singular_queries_using_multiple_post_type_should_respect_post_type_read_private_posts_cap() {
+               wp_set_current_user( 0 );
+
+               register_post_type(
+                       'wptests_pt1',
+                       array(
+                               'exclude_from_search' => false,
+                               'capabilities'        => [
+                                       'read_private_posts' => 'read_private_pt1s',
+                               ],
+                       )
+               );
+
+               register_post_type(
+                       'wptests_pt2',
+                       array(
+                               'exclude_from_search' => false,
+                       )
+               );
+
+               $post_ids = array();
+
+               $post_ids['wptests_pt1_p1'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt1_p2'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt2_p1'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $post_ids['wptests_pt2_p2'] = $this->factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => 'any',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+
+               $u = $this->factory->user->create();
+
+               wp_set_current_user( self::$subscriber_user_id );
+               $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => [ 'wptests_pt1', 'wptests_pt2' ],
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>