Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to use this code below to auto-refresh comments but at the moment it is displaying all comments. I would like to only show comments for the current post. I am not very experienced with SQL and don't know how to go about doing this:

<?php
function dp_recent_comments_ajax() {
        $no_comments = 5;
    $comment_len = 300;
        global $wpdb;

    $request = "SELECT * FROM $wpdb->comments";
    $request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
    $request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
    $request .= " ORDER BY comment_date DESC LIMIT $no_comments";

    $comments = $wpdb->get_results($request);
    if ($comments) {
        foreach ($comments as $comment) {
            ob_start();
            ?>
                <li>
                    <a style="font-size:12px "href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?> on <?php echo $comment->comment_date; ?> :</a><br />
                    <span class="recent-comment-text" style="font-size:11px;font-style:italic"><?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?>..</span>
                </li>
            <?php
            ob_end_flush();
        }
    } else {
        echo '<li>'.__('No comments', 'my-first-wp-theme').'</li>';
    }
    die(); // this is needed for ajax to work properly
}
function dp_get_author($comment) {
    $author = "";

    if ( empty($comment->comment_author) )
        $author = __('Anonymous', 'my-first-wp-theme');
    else
        $author = $comment->comment_author;

    return $author;
}
add_action('wp_ajax_my_recent_comments', 'dp_recent_comments_ajax');
add_action('wp_ajax_nopriv_my_recent_comments', 'dp_recent_comments_ajax'); //for users that are not logged in
share|improve this question
4  
Why don’t you use get_comments()? –  toscho Nov 20 '12 at 12:28
1  
What @toscho said. Is there a particular reason you're trying to use a raw SQL query (which actually renders this question off topic)? –  Chip Bennett Nov 20 '12 at 12:29
    
Apologies, I didn't realise this was off topic. No real reason for using SQL, I'm not sure how to go about doing this with get_comments(). I will give it a go though, I'm just trying to get the comments to auto refresh without using a plugin. –  jaffa Nov 20 '12 at 16:38

1 Answer 1

Send your post id in the function and it will look something like this

<?php
 function dp_recent_comments_ajax($ID) {
    $no_comments = 5;
    $comment_len = 300;
    global $wpdb;

$request = "SELECT * FROM $wpdb->comments";
$request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
$request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
$request .= " AND ID='{$ID}' ";
$request .= " ORDER BY comment_date DESC LIMIT $no_comments";
share|improve this answer
    
Thank you Mubbashar for the quick reply. I tried the suggestion above and it no longer displays any comments. –  jaffa Nov 20 '12 at 14:19
    
I tried this solution and it's working fine for me. Are you sending ID in the function whom comments you want to get? –  Mubbashar Nov 20 '12 at 18:00
    
no not exactly. I wish to retrieve the comments for the specific post that is being viewed. This value would change depending on which post you click and that is what I am struggling with –  jaffa Nov 20 '12 at 20:03
    
add_action('wp_ajax_my_recent_comments', 'dp_recent_comments_ajax'); add_action('wp_ajax_nopriv_my_recent_comments', 'dp_recent_comments_ajax'); As you are making ajax request you should send the Id of the post being displayed from your javascript code to server and then get the Id in dp_recent_comments_ajax() function using $ID = $_REQUEST['ID_you_send']; –  Mubbashar Nov 20 '12 at 20:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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