[wp-trac] [WordPress Trac] #34442: How to target only the get_comments() query in comment_template()

WordPress Trac noreply at wordpress.org
Sun Oct 25 23:30:13 UTC 2015


#34442: How to target only the get_comments() query in comment_template()
-------------------------+-----------------------------
 Reporter:  birgire      |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Comments     |    Version:  4.3.1
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 It would be handy to be able to target only the {{{get_comments()}}} query
 within the {{{comment_template()}}}, to avoid messing around with other
 {{{get_comments()}}} calls, e.g. in the widgets.

 == Suggestion A) ==

 We can do this to target the main query:

 {{{
     add_action( 'pre_get_posts', function( \WP_Query $q ) {
         if( $q->is_main_query() ) {
             // stuff here
         }
     });
 }}}

 so it would be great if we could use:

 {{{
     add_action( 'pre_get_comments', function( \WP_Comment_Query $q ) {
         if( $q->is_main_query() ) {
             // stuff here
         }
     });

 }}}

 We could also call that method {{{is_main_comment_query()}}} or
 {{{is_comment_template_query()}}}.

 How could we construct such a method?

 It might be just a query variable

 {{{
      $args = array(
         'order'    => 'ASC',
         'post_id' => $post->ID,
         'status'   => 'approve',
         'comment_template_query' => true
     );
 }}}

 I'm not sure if that would be a good idea, so maybe we could find a way to
 create and compare it to an object like {{{$wp_the_comment_query}}},
 similar to the idea within the {{{is_main_query()}}} method of
 {{{WP_Query}}}?

 == Suggestion B) ==

 Another approach would be to introduce an argument filter, e.g.
 {{{comment_template_query_args}}}, for the {{{get_comments()}}} within the
 {{{comment_template()}}} function:

 {{{
     $comments = get_comments( $args = apply_filters(
 'comment_template_query_args', $args ) );
 }}}

 This might be an easier approach, to start with.


 == Current workarounds ==

 We could of course modify our theme by adding:

 {{{
     add_action( 'pre_get_comments', 'some_callback' );
 }}}

 just before {{{comment_template()}}} is called and then use:

 {{{
     function some_callback( \WP_Comment_Query $q ) {
         // Run only once
         remove_action( current_action(), __FUNCTION__ );

         // some stuff ...
     }
 }}}

 but what if we would like this done through a plugin?

 A possible workarounds would be to match the default query variables
 within {{{pre_get_comments}}}.

 Maybe there's a much better way, or something I'm missing within the
 current setup ;-)

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


More information about the wp-trac mailing list