0

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="wantto">Tag your comment so other users can find your post.<br>I want to</label>';
    echo '<select id="wantto" name="wantto" class="myclass">';
        echo '<option value="---">---</option>';
        echo '<option value="BUY">BUY</option>';
        echo '<option value="SELL">SELL</option>';
        echo '<option value="TRADE">TRADE</option>';
        echo '<option value="TALK">TALK</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="wantosec">I 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.

The buttons section

1 Answer 1

0

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;
    
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.