0

I have modified the comment query arguments in comment-template.php from

$author_id = get_query_var( 'author_id', 1 );
        $comment_args = array(
                'order'   => 'ASC',
                'orderby' => 'comment_date_gmt',
                'status'  => 'approve',
        );
    $comments = get_comments( $comment_args );

to the following code below

$author_id = get_query_var( 'author_id', 1 );
    if($author_id == '') {
        $comment_args = array(
                'order'   => 'ASC',
                'orderby' => 'comment_date_gmt',
                'status'  => 'approve',
        );
    }
    else { 
        $comment_args = array(
                'order'     => 'ASC',
                'orderby'   => 'comment_date_gmt',
                'status'    => 'approve',
                'meta_key'  => 'custom_author_id',
                'meta_value'=> $author_id,
            );
    }
    $comments = get_comments( $comment_args );

I would like to know how to modify the number of comments being diplayed i.e. get_comments_number()

  • Are you talking about modifying the core file ./wp-includes/comment-template.php? – bosco Sep 11 '15 at 20:05
  • yes....i have modified that file – Shahrukh Khan Sep 11 '15 at 20:06
4

Refrain from editing core files

WordPress was developed with extensibility in mind, and provides many ways to change the appearance and functionality of an installation without altering the WordPress software itself (mostly using Hooks in conjunction with Plugins and Themes). The software is comprised of it's "core files", which are generally everything outside of the ./wp-content directory except ./wp-config.php, and any directory-level server configuration files (.htaccess, etc.).

Editing the core files of a WordPress installation is typically neither necessary nor maintainable, and as such is strongly discouraged unless you can find no other means to achieve your ends and are familiar with the consequences of doing so; these include automatic updates overwriting your changes, the WordPress community largely offering no support for your changes, and perhaps most importantly, you run a much larger risk of introducing new security vulnerabilities into your WordPress installation.

Rather than altering the core file ./wp-includes/comment-template.php, a more maintainable solution would be to create a comments.php template file within your custom theme (or a child theme of your current theme if was developed by a third-party) and use WP_Comment_Query in it to specify your new arguments. Use the number and offset arguments to specify a desired range of comments.

Template File ./wp-content/themes/wpse202457_theme/comments.php:

<?php
  $author_id = get_query_var( 'author_id', '' );
  $comments = array();

  if( $author_id != '' ) {
    $comment_args = array(
      'order'      => 'ASC',
      'orderby'    => 'comment_date_gmt',
      'status'     => 'approve',
      'meta_key'   => 'custom_author_id',
      'meta_value' => $author_id,
      'number'     => 20, // Retrieve 20 comments...
      'offset'     => 40  // ...starting from the 40th comment.
    );

    $comment_query = new WP_Comment_Query;
    $comments = $comment_query->query( $comment_args );
  }


  if( ! empty( $comments ) ) {
    // The Comments Loop
    foreach( $comments as $comment ) {
      echo( '<p>' . $comment->comment_content . '</p><br/><br/>' );
    }
  }
  else {
    echo( '<p>No comments found</p>' );
  }
?>

Check out this excellent question for the way to handle breaking comments up into pages (pagination) using the number and offset arguments dynamically. Dig into other themes' comments.php template file for some example markup.

Instead of creating a new theme template, you could alternately place your substitute comments template in a plugin, and use the comments_template filter to redirect comment template inclusions to it:

Plugin File ./wp-content/plugins/wpse202457_comments_plugin/wpse202457CommentsPlugin.php:

function wpse202457_comments_template( $comment_template ) {
  // Redirect ALL comment template inclusions to "./wp-content/plugins/wpse202457_comments_plugin/comments.php"
  return dirname(__FILE__) . '/comments.php';
}

add_filter( 'comments_template', 'wpse202457_comments_template' );

ADDENDUM

In response to the comments, as @Rarst suggested, the get_comments_query() function will only return the number of comments recorded for the post object displayed in the main query.

To get the number of comments for your altered query, simply count() the results of the query, i.e.:

$comment_query = new WP_Comment_Query;
$comments = $comment_query->query( $comment_args );
$comment_count = count( $comments );

However, if you are using the number argument to specify the number of comments you wish to retrieve, then $comment_count will always be equal to, or less than number. To get the total number of comments that match your arguments, you will either need to omit the number argument and count() the results, or set the count argument to true, in which case the query will return the number of matching comments without returning the comments themselves:

$author_id = get_query_var( 'author_id', 1 );
$comment_args = array(
  'status'     => 'approve',
  'meta_key'   => 'custom_author_id',
  'meta_value' => $author_id,
  'count'      => true // Return the NUMBER of matching comments, not the matching comment objects themselves
);

$comment_query = new WP_Comment_Query;
$comment_count = $comment_query->query( $comment_args );

If you don't plan on doing anything else with the WP_Comment_Query object, you may want to create some convenience functions and put them in your plugin, or your custom theme's functions.php for quick access. Something along the lines of:

// Get comments with a custom_author_id meta-value of $author_id
function wpse202471_get_author_comments( $author_id = null, $args = array() ) {
  // If an author id was not passed, see if there's one in the query vars
  if( ! isset( $author_id ) )
    $author_id = get_query_var( 'author_id', null );

  // If no author id was provided, return a negative result
  if( ! isset( $author_id ) ) {
    if( isset( $args[ 'count' ] ) && $args[ 'count' ] )
      return 0;

    return array();
  }

  // Merge query argument arrays
  $comment_args = array_merge(
    array(    // Default values
      'order'      => 'ASC',
      'orderby'    => 'comment_date_gmt',
      'status'     => 'approve'
    ),
    $args,    // Any arguments passed to the function
    array(    // Merge meta-arguments last, so nothing can overwrite them
      'meta_key'   => 'custom_author_id',
      'meta_value' => $author_id
    )
  );

  // Perform the query and return the results
  $comment_query = new WP_Comment_Query;
  return $comment_query->query( $comment_args );
}

// Get the total number of comments with a custom_author_id meta-value of $author_id
function wpse202471_get_author_comment_count( $author_id = null, $args = array() ) {
  $args[ 'count' ] = true;

  // Remove query arguments that would prevent the proper count from being returned
  if( isset( $args[ 'number' ] ) )
    unset( $args[ 'number' ] );

  if( isset( $args[ 'offset' ] ) )
    unset( $args[ 'offset' ] );

  return wpse202471_get_author_comments( $author_id, $args );
}
  • This error is displayed Fatal error: Call to a member function query() on null on the line $comments = $comments_query->query( $args ); – Shahrukh Khan Sep 11 '15 at 22:11
  • Thanks the code worked.....but how am i supposed to display the no of comments – Shahrukh Khan Sep 11 '15 at 22:38
  • Please help me with number of comments – Shahrukh Khan Sep 11 '15 at 22:46
  • i hope you are following with comments – Shahrukh Khan Sep 11 '15 at 23:08
  • Updated to add comment counting and a couple convenience functions under ADDENDUM – bosco Sep 12 '15 at 3:23
0

get_comments_number() does not rely on any comment query, it simply reports the number of comments stored persistently in post object (comment_count property).

You have omitted in your example how exactly query itself looks like. If you are using actual WP_Comment_Query object then its comments property will have array of results you could count(). If using API functions might get messier than that.

  • Is this the code....could you give me full fleged code – Shahrukh Khan Sep 11 '15 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.