get_page_of_comment

Calculate what page number a comment will appear on for comment paging.

Description

(int|null) get_page_of_comment( (int) $comment_ID, (array) $args = array() ); 

Returns (int|null)

Comment page number or null on error.

Parameters (2)

0. $comment_id (int)
The comment id.
1. $args — Optional. (array) => array()
Array of optional arguments.

Options

  • type (string) => 'all'

    Limit paginated comments to those matching a given type. Accepts comment,, trackback , pingback., pings (trackbacks and pingbacks), or all.

  • per_page (int) => 'comments_per_page'

    Per-page count to use when calculating pagination.

array(

    /**
     * Limit paginated comments to those matching a given type. Accepts 'comment', 'trackback',
     * 'pingback', 'pings' (trackbacks and pingbacks), or 'all'.
     *
     * @type string
     * @default 'all'
     */
    'type' => 'all',

    /**
     * Per-page count to use when calculating pagination.
     *
     * @type int
     * @default 'comments_per_page'
     */
    'per_page' => 'comments_per_page'
);        


Usage

  1. if ( !function_exists( 'get_page_of_comment' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // The comment id. 
  5. $comment_id = -1; 
  6.  
  7. // Array of optional arguments. 
  8. $args = array( 
  9. 'type' => 'all', 
  10. 'per_page' => 'comments_per_page' 
  11. ); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_page_of_comment($comment_id, $args); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function get_page_of_comment( $comment_ID, $args = array() ) { 
  2. global $wpdb; 
  3.  
  4. $page = null; 
  5.  
  6. if ( !$comment = get_comment( $comment_ID ) ) 
  7. return; 
  8.  
  9. $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); 
  10. $args = wp_parse_args( $args, $defaults ); 
  11. $original_args = $args; 
  12.  
  13. // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option. 
  14. if ( get_option( 'page_comments' ) ) { 
  15. if ( '' === $args['per_page'] ) { 
  16. $args['per_page'] = get_query_var( 'comments_per_page' ); 
  17.  
  18. if ( '' === $args['per_page'] ) { 
  19. $args['per_page'] = get_option( 'comments_per_page' ); 
  20.  
  21. if ( empty($args['per_page']) ) { 
  22. $args['per_page'] = 0; 
  23. $args['page'] = 0; 
  24.  
  25. if ( $args['per_page'] < 1 ) { 
  26. $page = 1; 
  27.  
  28. if ( null === $page ) { 
  29. if ( '' === $args['max_depth'] ) { 
  30. if ( get_option('thread_comments') ) 
  31. $args['max_depth'] = get_option('thread_comments_depth'); 
  32. else 
  33. $args['max_depth'] = -1; 
  34.  
  35. // Find this comment's top level parent if threading is enabled 
  36. if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) 
  37. return get_page_of_comment( $comment->comment_parent, $args ); 
  38.  
  39. if ( 'desc' === get_option( 'comment_order' ) ) { 
  40. $compare = 'after'; 
  41. } else { 
  42. $compare = 'before'; 
  43.  
  44. $comment_args = array( 
  45. 'type' => $args['type'],  
  46. 'post_id' => $comment->comment_post_ID,  
  47. 'fields' => 'ids',  
  48. 'count' => true,  
  49. 'status' => 'approve',  
  50. 'parent' => 0,  
  51. 'date_query' => array( 
  52. array( 
  53. 'column' => "$wpdb->comments.comment_date_gmt",  
  54. $compare => $comment->comment_date_gmt,  
  55. ),  
  56. ); 
  57.  
  58. $comment_query = new WP_Comment_Query(); 
  59. $older_comment_count = $comment_query->query( $comment_args ); 
  60.  
  61. // No older comments? Then it's page #1. 
  62. if ( 0 == $older_comment_count ) { 
  63. $page = 1; 
  64.  
  65. // Divide comments older than this one by comments per page to get this comment's page number 
  66. } else { 
  67. $page = ceil( ( $older_comment_count + 1 ) / $args['per_page'] ); 
  68.  
  69. /** 
  70. * Filters the calculated page on which a comment appears. 
  71. * @since 4.4.0 
  72. * @since 4.7.0 Introduced the `$comment_ID` parameter. 
  73. * @param int $page Comment page. 
  74. * @param array $args { 
  75. * Arguments used to calculate pagination. These include arguments auto-detected by the function,  
  76. * based on query vars, system settings, etc. For pristine arguments passed to the function,  
  77. * see `$original_args`. 
  78. * @type string $type Type of comments to count. 
  79. * @type int $page Calculated current page. 
  80. * @type int $per_page Calculated number of comments per page. 
  81. * @type int $max_depth Maximum comment threading depth allowed. 
  82. * } 
  83. * @param array $original_args { 
  84. * Array of arguments passed to the function. Some or all of these may not be set. 
  85. * @type string $type Type of comments to count. 
  86. * @type int $page Current comment page. 
  87. * @type int $per_page Number of comments per page. 
  88. * @type int $max_depth Maximum comment threading depth allowed. 
  89. * } 
  90. * @param int $comment_ID ID of the comment. 
  91. */ 
  92. return apply_filters( 'get_page_of_comment', (int) $page, $args, $original_args, $comment_ID );