How to filter comments by comment_meta

EDIT: Yay, solved it! I needed to use new WP_Comment_Query( $args ) and set my $args to only include the current post and the meta chosen. Thanks!

Thanks to the wonderful help of the community here, I have created a dropdown menu above my comments section to choose between BUY | SELL | TRADE | TALK.

EDIT: I've changed my comment.php file. It now filters by comment_meta - YAY! Except, it gets ALL comments with this meta value. How do I constrain it to JUST comments on this page?

Right now, i have them adding ?cmeta=ALL, etc to the url. I don't like this because it reloads the page. I suppose I'll need some javascript? Is there another way to get buttons to filter the comments below?

EDIT: got the buttons in the right spot.

Thanks in advance, everyone!

Here's the code from my functions.php


/*
 * This will add the fields to the comment form
*/
function wpse406058_custom_comment_fields() {

    echo 'p class=comment-form-wantto';
    echo 'label for=wanttoTag your comment so other users can find your post.brI want to/label';
    echo 'select id=wantto name=wantto class=myclass';
        echo 'option value=------/option';
        echo 'option value=BUYBUY/option';
        echo 'option value=SELLSELL/option';
        echo 'option value=TRADETRADE/option';
        echo 'option value=TALKTALK/option';
    echo '/select';
  
      
}
add_action( 'comment_form_logged_in_after', 'wpse406058_custom_comment_fields' );
add_action( 'comment_form_after_fields', 'wpse406058_custom_comment_fields' );

/*
 * This will field value as comment meta
*/
function wpse406058_save_custom_field($comment_id) {

    if ( isset($_POST['wantto'])  !empty($_POST['wantto']) ) {
        $wantto = sanitize_text_field($_POST['wantto']);
        update_comment_meta( $comment_id, 'wantto', $wantto );
    }
    
}
add_action( 'comment_post', 'wpse406058_save_custom_field' );


function wpse406058_display_comment_meta( $comment_text ) {
    
    $wantto = get_comment_meta( get_comment_ID(), 'wantto', true );
    
    if ( isset($wantto)  !empty($wantto) ) {
        
        $wanttotext = 'p class=wantosecI want to ' . esc_html($wantto) . '/p';
        
        $comment_text = $wanttotext . $comment_text;
    }
   echo 'div class=container';
    return $comment_text;
    echo '/div';
}
add_filter( 'comment_text', 'wpse406058_display_comment_meta' );

I'm calling it on each page with

?php comments_template(); ? 

And here's my comments.php

?php
comment_form();
if (have_comments()) : ?
    div class=container
        div class=btn-group flex
            button type=button onclick=window.location.href='?php echo the_permalink();?/?cmeta=ALL'SHOW ALL/button
            button type=button onclick=window.location.href='?php echo the_permalink();?/?cmeta=BUY'BUY/button
            button type=button onclick=window.location.href='?php echo the_permalink();?/?cmeta=SELL'SELL/button
            button type=button onclick=window.location.href='?php echo the_permalink();?/?cmeta=TRADE'TRADE/button  
            button type=button onclick=window.location.href='?php echo the_permalink();?/?cmeta=TALK'TALK/button  
        /div  
    /div
    ?php
    if(isset($_GET['cmeta'])){
        $commentmeta=$_GET['cmeta'];
        $wantto= get_comment_meta( $comment_id, 'wantto', true );
            $args = array(
            'post_id' = get_the_ID(),
            'meta_query' = array(
            
                array(
                    'key' = 'wantto',
                    'value' = $commentmeta,
                ),
                
            )
        );
        $comment_query = new WP_Comment_Query( $args );
        $comments = $comment_query-comments;
            echo 'ol class=post-comments';
                wp_list_comments(array(
                    'style'       = 'ol',
                    'short_ping'  = true,
                ));
                echo $wantto;
                
        echo '/ol';
        /* much simpler one to style probably
                        if ( $comments ) {
                    foreach ( $comments as $comment ) {
                        echo 'p' . $comment-comment_content . '/p';
                    }
                } else {
                    echo 'No comments found.';
                }
        */
    }else{

        $wantto= get_comment_meta( $comment_id, 'wantto', true );
        echo 'ol class=post-comments';
                wp_list_comments(array(
                    'style'       = 'ol',
                    'short_ping'  = true,
                ));
                echo $wantto;
                
        echo '/ol';
    }
    endif;
    $comment_id=get_comment_ID();
    echo $wantto;
    
?

Here's a screenshot of the buttons below the comment section.

Topic comment-meta comment-form comments Wordpress

Category Web


I needed to use new WP_Comment_Query and set my $args to show only this post, with the meta attached. Hope this helps someone!

<?php
comment_form();
if (have_comments()) : ?>
    <div class="container">
        <div class="btn-group flex">
            <button type="button" onclick="window.location.href='<?php echo the_permalink();?>/?cmeta=ALL'">SHOW ALL</button>
            <button type="button" onclick="window.location.href='<?php echo the_permalink();?>/?cmeta=BUY'">BUY</button>
            <button type="button" onclick="window.location.href='<?php echo the_permalink();?>/?cmeta=SELL'">SELL</button>
            <button type="button" onclick="window.location.href='<?php echo the_permalink();?>/?cmeta=TRADE'">TRADE</button>  
            <button type="button" onclick="window.location.href='<?php echo the_permalink();?>/?cmeta=TALK'">TALK</button>  
        </div>  
    </div>
    <?php
    if(isset($_GET['cmeta'])){
        $commentmeta=$_GET['cmeta'];
        $wantto= get_comment_meta( $comment_id, 'wantto', true );
            $args = array(
            'post_id' => get_the_ID(),
            'meta_query' => array(
            
                array(
                    'key' => 'wantto',
                    'value' => $commentmeta,
                ),
                
            )
        );
        $comment_query = new WP_Comment_Query( $args );
        $comments = $comment_query->comments;
            echo '<ol class="post-comments">';
                wp_list_comments(array(
                    'style'       => 'ol',
                    'short_ping'  => true,
                ));
                echo $wantto;
                
        echo '</ol>';
        /* much simpler one to style probably
                        if ( $comments ) {
                    foreach ( $comments as $comment ) {
                        echo '<p>' . $comment->comment_content . '</p>';
                    }
                } else {
                    echo 'No comments found.';
                }
        */
    }else{

        $wantto= get_comment_meta( $comment_id, 'wantto', true );
        echo '<ol class="post-comments">';
                wp_list_comments(array(
                    'style'       => 'ol',
                    'short_ping'  => true,
                ));
                echo $wantto;
                
        echo '</ol>';
    }
    endif;
    $comment_id=get_comment_ID();
    echo $wantto;
    
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.