[wp-trac] [WordPress Trac] #50521: comments_pre_query doesn't align with other pre_query filters

WordPress Trac noreply at wordpress.org
Wed Jul 1 09:35:59 UTC 2020


#50521: comments_pre_query doesn't align with other pre_query filters
--------------------------+-----------------------------
 Reporter:  dinhtungdu    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Comments      |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This is a follow-up to #45800.

 In 45800, if $comment_data isn't null, we return it without assigning it
 back to the `comment` property. This is inconsistent with other pre_query
 filters behavior of other objects (User, Term, ...). Also, it creates a
 minor inconvenience and possibly a performance issue when querying
 comments.

 For example, a theme/plugin hooks into `comments_pre_query`, and we use
 `WP_Comment_Query` to query comments.

 {{{#!php
 <?php

 $args = array(
     'type' => 'comment',
 );

 // The Query
 $comments_query = new WP_Comment_Query( $args );
 }}}

 To retrieve comments, we need to use `$comment_query->get_comments()`.
 Beside, when we initialize a new instance of `WP_Comment_Query`, the
 `WP_Comment_Query::query()` method get executed. So we're running
 `query()` method two times to get comments from the query. We can't use
 `$comments_query->comments` because it's `null`.

 IMO, the solution here is assigning the comment data to `comments`
 property before the short-circuit.

 Note that the doc of WP_Comment_Query is incorrect. We always receive an
 instance of WP_Comment_Query after the initialization, not 'an array of
 comments' as the documentation states here:
 https://developer.wordpress.org/reference/classes/wp_comment_query/#basic-
 usage

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50521>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list