将 Children 包含在 WP_Query 中

Include Children In WP_Query

我需要将 children 条评论(回复)添加到我使用元查询过滤的评论中。

如果我过滤评分为 3/5 的评论,我需要在查询中包含它们的 children(即使 children 与元查询不匹配)。

$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']
        )
    )
) );

有没有办法 "force include" children 不匹配初始查询?

(要实时查看问题,请尝试在此页面上按 3 星过滤评论,并注意评论回复如何 包含在过滤器中:https://herbalnitro.com/product/extreme-energy/)

您获得的唯一评论具有特定元但子评论显然没有继承此元的问题。所以你需要分两步来做:1)用meta获取评论。 2) 获取带有 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);