List Comments with #tag in Comment Content
-
Hi, I’m in the middle of adding support for tagging comments in WordPress. I’ve currently got a working version, but it’s not optimized. The current approach fetches all the comments and display the ones matching the tag. For example, a comment with #tag will display on the /tag/ page. Has anyone created a custom query to fetch _only_ the comments matching the tag? Here’s the unoptimized code I’ve got so far:
<?php $comments = get_comments(); foreach ($comments as $comment) { if (preg_match('/#'.$tag.'\b/i', $comment->comment_content)){ echo "<h3>Comments tagged \"#$tag\"</h3>"; break; } } ?> <ol class="commentlist"> <?php $comments = get_comments(); foreach ($comments as $comment) : ?> <?php if (!preg_match('/#'.$tag.'\b/i', $comment->comment_content)){ //display comments with our hash tag only continue; } ?>
Thanks!
- The topic ‘List Comments with #tag in Comment Content’ is closed to new replies.