Home > Enterprise >  How apply comments on one post to all posts of the same category in Wordpress
How apply comments on one post to all posts of the same category in Wordpress

Time:11-10

I have created a custom post type for events (performaces of theater plays). Event categories denote plays and the events are performances of the plays. I would like user comments on individual performances (i.e. posts of the event custom-post-type) to apply to the category and appear on all other events of the same category. The question is how to best achieve this.

One (quite bad) idea would be to use the comment_post hook and attach a copy of the comment to all posts of the same category. But first this would not apply to new posts of the category (unless comments are copied when a new post is first saved), change of category would need to be taken care of, ... and it does not seem very elegant to duplicate comments this way.

Another idea would be to use the comment_post hook and attach the comment_id as a termmeta to the category and develop a different comments.php to pick up the comments from the category. Seems a bit complicated but not undoable.

Any ideas?

CodePudding user response:

Would something like this get you started?

function get_show_comments() {

    // Get the current post's categories.
    $categories = get_the_category( get_the_id() );

    // Get the category IDs.
    foreach ( $categories as $category ) {
        $category_ids[] = $category->cat_ID;
    }

    // Format for query.
    $category_ids = implode(',', $category_ids);

    // Get all posts with those categories.
    $events = get_posts( 'cat=' . $category_ids );

    // Put all their comments into an array.
    foreach ( $events as $event ) {
        $comments[] = get_comments( $event );
    }

    // Somewhere in here you'd presumably want to sort the comments by date, but I have to get to bed, lol.

    return $comments;

}

I'm sure this could be optimized a bit, but hope this helps.

CodePudding user response:

I built my solution based on the proposal from Shoelaced. Since I use a custom taxonomy for the categories it is a little bit different:

function get_show_hello_comments() {

  global $post_id;
  // Get the current post's categories.
  // Need to use get_the_terms since we use a custom taxonomy
  $categories = get_the_terms($post_id, 'hello_event-category');

  // Get the category IDs.
  foreach ( $categories as $category ) {
      $term_ids[] = $category->term_id;
  }

  // Get all posts with those categories.
  $events = get_posts(
    array(
      'posts_per_page' => -1,
      'post_type' => 'hello_event',
      'tax_query' => array(
        array(
          'taxonomy' => 'hello_event-category',
          'field' => 'term_id',
          'terms' => $term_ids,
        )
      )
    )
  );

  // Put all their comments into an array.
  $comments = [];
  foreach ( $events as $event ) {
    $comments = array_merge($comments, get_comments( $event ));
  }
  // Sort ascending on dates
  usort($comments, function($a, $b) {return strcmp($a->comment_date, $b->comment_date);});

  return $comments;
}

I don't want to allow replies to comments, which makes the display quite simple. In my child theme of OceanWP I include a slightly modified comments.php which tests if the post_type is my custom post type, in which case:

$hello_comments = get_show_hello_comments($post->ID);

...

echo '<ol >';

$args= array('depth'=> 1,'max_depth' => 1);
foreach($hello_comments as $comment) {
  oceanwp_comment($comment, $args, 1);
}
echo '</ol>'
  • Related