How to filter comments by comment_meta

Question

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.

I now want to create buttons to filter the data by this comment_meta below. Oh and I should add an ALL button, to go back to showing all types. Each button would only show comments tagged with the comment meta.

I know how to create buttons. I think I need to have each button (if clicked) call a wp_query that shows only comments with comment_meta_type = "BUY" for instance. I’m just not clear how to word that. How would I write out that bit of code?

Where would I put the to have it display below “POST COMMENT” button and above the “Comments”? Does this need to be another function with something like add_action( 'comment_post_after', 'wpse406058_save_custom_field' );? I’m totally guessing with this, but I think I’m on the right track.

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

<?
comment_form();

if (have_comments()) : 

    $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 mockup of where I’d like the buttons to go (between post comment and the first reply).

Here's what I'd like to do.

0
RCOTWUnite 2 months 2022-05-27T12:06:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse