4

For example post has comment area with 3 differnet tabs, e.g comments, reviews and questions.

I've looked into it for some time know but I haven't came up with a perfect plan.. I figured it's time to address WordPress gods here.


Concept is easy:

       ------------------------------------------------------------------
          1. category link      2. category link     3. category link
       ------------------------------------------------------------------


                        Current category comments


       ------------------------------------------------------------------

If one of the links is clicked, run (via ajax or not, matter of taste) query that get X category comments.

I've looked into WP_Comment_Query() but Im not sure how to handle this.. There's no category or type parameter, so I figured that comment meta is probably the best way to go..


Adding category when commenting:

//To functions.php or create a plugin

add_action( 'comment_post', 'add_comment_category' );

function add_comment_category( $comment_id ) {

    add_comment_meta( 

        $comment_id,
        'my_comment_category', 
        $_POST['hidden-input-category-value'] 
    );
}
  • Front-end is pretty much covered but admin area is whole different story and Im not sure that I should add hidden input to everywhere you can comment (edit post page, comments page etc). Is there a better alternative to that? How else could I pass current category value?
  • Also hidden input can be altered by mean people and break the system - not good at all.
  • Im pretty sure that I should do commenting via ajax so that I wouldn't have to worry about opening wrong tab when page reloads.

Admin area output:

I don't need to sort them by categories in admin area, I'll just add new column to comments list that outputs the category just in case, maybe there will be times when I need to know.


Front-end output:

//Create new template for this comment system and use get_template_part()

//Or add it straight to post or page template

$args = array(

    'post_id'    => $current_post_id,
    'meta_query' => array(
        array(
            'key'   => 'my_comment_category',
            'value' => 'review'
        )
     )
 );

$comments = new WP_Comment_Query( $args );

//Output
while( $comments->have_comments() ) { 

    $comments->the_comment();

    //Output
} 
else {

    //No comments output
}

This is inspired by my usual WP_Query() build. I imagine there's consistency in WordPress and all different query results can be looped the same way and used rather similar code ( e.g the_post() == the_comment() or have_posts() == have_comments() ).


  • Im not sure if replying works by default
  • Im not sure if nested comments work by default
  • Im not sure if there are any other poblems that I don't see at the moment

I guess the right question now is: Is it worth it? Would it perform much worse than default comments? Any alternatives?


Any more questions or ideas? Good, let me know and I'll update the question.

  • 1
    If I recall correctly, WooCommerce is using various comment types (comment_type), you could look into that for implementation ideas. Update: searched and found e.g. this thread on this site. – birgire Feb 25 '16 at 15:34
1

Comments are kind of "limited" in WordPress. You have three different types for them as get_comment_type() shows nicely and you can not extend that list.

So, adding meta values to your comments is the easiest, and probably "most standard" way, to be able to distinguish different "types" of comments.

Handling the comment in- and output stays basically the same, except that you have to add a meta value to some of them. As you have already shown this is done quite easily.

Regarding your questions:

Is it worth it?

I think your solution is the best possible way without tinkering too much in the database, creating new structures and stuff like that. Your solutions just simply doesn´t need that. I like that because it improves compatibility.

Would it perform much worse than default comments?

I don´t think it will perform worse at all. You are basically just adding another parameter to the database query/doing another join and databases are fast as hell for things like this. Try to implement caching when you get more load on your site and it should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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