searching posts and comments
-
As a novice php hacker, I’m pretty impressed that I managed to get the search feature to search both posts and comments at once. But since this wasn’t the standard, it’s possible there is a good reason for not doing this. So can anyone tell me if this is ok? The functionality I changed is all in index.php as follows (mostly cribbed from edit-comments.php):
Replace:
<?php endforeach; else: ?>
'<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>
</div>'
with
'<?php endforeach; endif; ?>
<?php if (isset($_GET['s'])) : ?>
<?php $s = $wpdb->escape($_GET['s']);
$comments = $wpdb->get_results("SELECT * FROM $tablecomments WHERE
comment_author LIKE '%$s%' OR
comment_author_url LIKE ('%$s%') OR
comment_content LIKE ('%$s%')
ORDER BY comment_date DESC"); ?>
<?php if ($comments) : printf(__('Search results for %s in comments:'),$s) ; foreach ($comments as $comment) : ?>
<?php $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $tableposts WHERE ID = $comment->comment_post_ID"));
$comment_status = wp_get_comment_status($comment->comment_ID);
if ('unapproved' != $comment_status) : ?>
<?php echo '- '; ?>
<?php $post_title = $wpdb->get_var("SELECT post_title FROM $tableposts WHERE ID = $comment->comment_post_ID");
$post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; ?>
comment_post_ID; ?>#comment-<?php echo $comment->comment_ID; ?>"><?php _e('View comment'); ?> by <?php if ($comment->comment_author_url) { ?> "> <?php comment_author() ?> <?php } else { ?> <?php comment_author() ?> <?php } ?> on comment_post_ID; ?>"><?php printf(__('“%s”'), stripslashes($post_title)); ?>
<?php endif; ?>
<?php endforeach; else: ?>
<?php _e('Sorry, no posts or comments matched your criteria.'); ?>
<?php endif; ?>
<?php else: ?>
<?php _e('Sorry, no posts or comments matched your criteria.'); ?>
<?php endif; ?>
</div>'
- '; ?>
- The topic ‘searching posts and comments’ is closed to new replies.