• Anonymous User 6087458

    (@anonymized-6087458)


    I am using the Search Everything plugin to search through comments in each post and that is working nicely. The problem though is that search results are not showing the comment with the matching search result, I just have a preview of the post itself showing.

    Can someone please give a guide as to how to get the correct comment also showing?

    https://wordpress.org/plugins/search-everything/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Anonymous User 6087458

    (@anonymized-6087458)

    Just to update; has nobody really needed this as it would seem like a fairly obvious request…

    Hey,

    thanks for reaching out!
    This is a quick workaround as we probably won’t implement this feature in the plugin. Go to “Appearance”->”Editor”-> select your theme and find e.g. “content-search.php” file (for the Twenty Fifteen theme). There insert the following (I inserted it after the <?php the_excerpt(); ?> but you can put it somewhere else if you wish to):

    <?php foreach(get_comments() as $comment): ?>
    		<?php if (strpos($comment->comment_content, $_GET["s"]) !== FALSE): ?>
    			<?php echo $comment->comment_content; ?>
    		<?php endif; ?>
    	<?php endforeach; ?>

    Don’t forget to update the file. Also make a backup before you make any changes – better to be safe than sorry.

    Let me know how it goes, take care and have a lovely day,
    Petra

    Thread Starter Anonymous User 6087458

    (@anonymized-6087458)

    Thanks for this it led me in the right direction.

    I’ve actually used the following (in case its useful for anyone else) as I need to show only the comment in the result if there is a match and I wanted to prefix the comment with some text stating its been found there…

    $matchinComments=false;
    foreach(get_comments(get_the_id()) as $comment):
    	if ( strpos($comment->comment_content, $_GET["s"]) !== FALSE && $comment->comment_post_ID==get_the_id()) {
    		echo "<strong>Match in comments:</strong><br>".$comment->comment_content;
    		$matchinComments=true;
    	}
    endforeach; 
    
    if ($matchinComments!=true){
    	echo get_the_excerpt();
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I can now search in comments but I need to show them in results’ is closed to new replies.