I need to include children comments (replies) to comments which I'm filtering with a meta query.
If I'm filtering comments that have a rating 3/5, I need to include their children with the query (even though the children don't match the meta query).
$comments = get_comments( array (
//'meta_key' => 'rating',
'order' => 'ASC',
'orderby' => 'date',
'post_id' => $_POST['post_id'],
'status' => 'approve',
'meta_query' => array(
array(
'key' => 'rating',
'value' => $_POST['rating']
)
)
) );
Is there a way to "force include" children that don't match initial query?
(To see the issue live, try to filter the reviews by 3 stars on this page, and notice how the review reply is not included in the filter: https://herbalnitro.com/product/extreme-energy/)
The issue that you get the only comment with specific meta but children comments do not inherit this meta obviously. So you need to do it in two steps: 1) get comments with meta. 2) get children comments for a parent with which is with meta.
// get comments with meta
$comments = get_comments( array (
'order' => 'ASC',
'orderby' => 'date',
'post_id' => $_POST['post_id'],
'status' => 'approve',
'meta_query' => array(
array(
'key' => 'rating',
'value' => $_POST['rating']
)
)
) );
// find children comments
$comments_children = array();
foreach ( $comments as $comment ) {
$comments_children += get_comments(array('parent' => $comment->comment_ID, 'status' => 'approve', 'hierarchical' => true));
}
// combine all comments
$comments = array_merge($comments, $comments_children);
// print comments template if needed
wp_list_comments(array(), $comments);