[wp-trac] [WordPress Trac] #43238: `suppress_filters` not set in `pre_get_posts` hook

WordPress Trac noreply at wordpress.org
Fri Jan 31 18:49:06 UTC 2020


#43238: `suppress_filters` not set in `pre_get_posts` hook
---------------------------+-----------------------
 Reporter:  adampatterson  |       Owner:  (none)
     Type:  defect (bug)   |      Status:  reopened
 Priority:  normal         |   Milestone:
Component:  Query          |     Version:  4.9.4
 Severity:  normal         |  Resolution:
 Keywords:  dev-feedback   |     Focuses:
---------------------------+-----------------------
Changes (by Giorgio25b):

 * keywords:   => dev-feedback
 * status:  closed => reopened
 * resolution:  invalid =>


Comment:

 Even though this ticket highlights a solution, it does not address the
 problem at its source.
 The reporter did experience and describe the correct scenario, but I found
 some other instances where the problem does not occur and it might be a
 problem with the `/wp-json/wp/v2/media` endpoint itself.
 The fully working function passes validation and control that the user can
 actually see `drafts`:
 {{{#!php
 <?php
 function show_draft_posts( $query ) {
     if ( is_admin() ) {
         return $query;
     }

     if ( user_can( wp_get_current_user(), 'administrator' ) ) {
         if ( array_key_exists( 'suppress_filters', $query->query_vars )
 and $query->query_vars['suppress_filters'] ) {
             return $query;
         }

         $query->set( 'post_status', [ 'publish', 'draft' ] );

         return $query;
     }
 }

 add_filter( 'pre_get_posts', 'show_draft_posts' );
 }}}

 Though when we pass the same function without the user validation, such
 as:
 {{{#!php
 <?php
 function show_draft_posts( $query ) {
     if (is_admin()) {
         return $query;
     }

     if (array_key_exists('suppress_filters', $query->query_vars) &&
 $query->query_vars['suppress_filters']) {
         return $query;
     }

     $query->set('post_status', ['publish', 'draft']);
     return $query;
 }

 add_filter( 'pre_get_posts', 'show_draft_posts' );
 }}}

 We only trigger an error in the `/wp-json/wp/v2/media` endpoint, which
 will return an empty array `[ ]`. The same issue should occur with `/wp-
 json/wp/v2/users` and `/wp-json/wp/v2/posts` for example but it does not,
 therefore I think that the `media` endpoint is either treated differently
 or there is something wrong somewhere that I can not pin down.

 I really hope this helps, thank you!

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


More information about the wp-trac mailing list