[wp-trac] [WordPress Trac] #45800: Add short circuits for WP_Comment_Query

WordPress Trac noreply at wordpress.org
Wed Mar 27 14:17:49 UTC 2019


#45800: Add short circuits for WP_Comment_Query
--------------------------------------+------------------------------
 Reporter:  spacedmonkey              |       Owner:  adamsilverstein
     Type:  enhancement               |      Status:  assigned
 Priority:  normal                    |   Milestone:  5.3
Component:  Comments                  |     Version:  3.1
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+------------------------------
Changes (by felipeelia):

 * keywords:  needs-patch => has-patch has-unit-tests


Comment:

 Just uploaded a patch with the necessary changes and a new unit test.
 FWIW, here is a callback that could be hooked to the new
 `comments_pre_query` filter and serve as an example:


 {{{#!php
 <?php
 function wp45800_comments_pre_query( $comments, $query ) {
         // Necessary to avoid an infinite loop for hierarchical queries.
         if ( ! empty( $query->query_vars['parent'] ) ) {
                 return [];
         }
         $comments = [
                 (object) [
                         'comment_ID'      => 100,
                         'comment_content' => 'Lorem Ipsum',
                 ],
                 (object) [
                         'comment_ID'      => 200,
                         'comment_content' => 'Ipsum lorem',
                 ],
         ];
         if ( $query->query_vars['count'] ) {
                 return count( $comments );
         }
         if ( 'ids' == $query->query_vars['fields'] ) {
                 return wp_list_pluck( $comments, 'comment_ID' );
         }
         return $comments;
 }
 add_filter( 'comments_pre_query', 'wp45800_comments_pre_query', 10, 2 );
 }}}

 Thank you, @adamsilverstein. Let me know if any change is needed :)

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


More information about the wp-trac mailing list