Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function _get_child_comments( $clauses ) {
- $clauses['where'] .= ' AND comment_parent > 0';
- return $clauses;
- }
- function get_comments_with_replies( $query ) {
- if ( empty( $query->query_vars['with_replies'] ) ) {
- return;
- }
- remove_filter( 'parse_comment_query', __FUNCTION__ );
- add_filter( 'comments_clauses', '_get_child_comments' );
- $child_comments = get_comments( $query->query_vars );
- remove_filter( 'comments_clauses', '_get_child_comments' );
- add_filter( 'parse_comment_query', __FUNCTION__ );
- $parent_comment_ids = array();
- foreach ( $child_comments as $comment ) {
- if ( ! in_array( $comment->comment_parent, $parent_comment_ids ) ) {
- $parent_comment_ids[] = $comment->comment_parent;
- }
- }
- $query->query_vars['comment__in'] = $parent_comment_ids;
- }
- add_action( 'parse_comment_query', 'get_comments_with_replies' );
- $comments = get_comments( array( 'post_id' => 123, 'with_replies' => true ) );
- echo '<pre>'; print_r( $comments ); echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement