Question:

Displaying comment rating stars in carousel

Jack: 5 days ago

I need a solution that will allow users rate page content within a comment.

After that problem comes when I need show that comments(with rating stars) in different spot on my site where I display all of them in a carousel.

for now I have carousel with all needed information(comment author name or piece of comment content) and I'm missing only this rating system implementation:

<?php 
    $args_count = array(
        'count' => true
    );
    $comments_query_count = new WP_Comment_Query;
    $comments_count = $comments_query_count->query( $args_count );

    $args = array(
    );
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );

    if ( $comments ) {
        for ($i=0;$i<$comments_count; $i++) { ?>
            <div>
                <div class="a24-carousel-item">
                    <div class="row">
                        <?php echo get_avatar($comments[$i]->comment_author_email);?>
                        <div class="HERE I NEED stars from specific comment"></div>
                        <p class="author"><?php echo $comments[$i]->comment_author ?></p>
                        <p class="date">Added: <?php echo $comments[$i]->comment_date ?></p>
                        <p class="associated-post"><?php echo get_the_title($comments[$i]->comment_post_ID) ?></p>
                        <p class="content">
                        <?php echo $comments[$i]->comment_content; ?>
                        </p>
                    </div>
                </div>
            </div>
        <?php 
        }
    }
?>

Does anyone know a way how to help me displaying comment ratings in my carousel?

Answer:
Bella: 5 days ago

If the comment rating is stored in the comment meta, then you can use:

$rating = get_comment_meta( get_comment_ID(), 'some-meta-key', true );

where you need to adjust the some-meta-key accordingly.

If the comment rating is automatically injected into the comment's text via the comment_text filter, then you need to use the comment_text() function to display it.

Here's an example:

if ( $comments ) {
    foreach( $comments as $comment ) {
        // ...
        comment_text( $comment );
    }
}

Note that here you don't need to run WP_Comment_Query twice, as you do in your snippet, just to get the number of comments.

There's also a core function for displaying star ratings: wp_star_rating() (https://developer.wordpress.org/reference/functions/wp_star_rating/)