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 currently loading a single post using ajax. While the post loading works ok, I cannot get the comments to load. Here is my code:

My javascript to load the post:

<script>
$(".view_post").click(function(e) {
    e.preventDefault();
    postid = $(this).attr("rel");
                $.ajax({
                    url:"/wp-admin/admin-ajax.php",
                    type:'POST',
                    data:'action=posts_open&postid='+postid,
                    success: function(html){
                        $("#b_contentwrapper").empty();
                        $("#b_contentwrapper").append(html);
                    }
                });
            });
</script>

The javascript goes through functions.php this way:

function implement_posts()
{
    //<?php
    get_template_part( 'loop', 'single' );
    die();
}

Now here is the code where I actually load my post content:

<?php
    $linkid = "p=".$_POST["postid"];
    $posti = new WP_Query($linkid);
    $posti->the_post();
    echo "Time: ";
    the_time('F jS, Y');
    echo "<br />";
    the_category(', ');
    echo "<br />";
    the_title();
    echo "<br />";
    the_content();
    echo "<br />";
    comment_form();
    ?>
    </div>
    <?php if (have_comments()) {
    echo "Comments ok";
    }
    else
    {
        echo "No comments";
    }
    ?>

Now, even for posts having comments I am getting "No comments" displayed. Everything else works correctly. Can anyone help me out?

Thank you.

share|improve this question
    
$linkid = "p=".$_POST["postid"]; is not verry sql injection safe. although wordpess checks this you might wanna do it yourself. –  RTB Jul 31 '12 at 22:49
add comment

3 Answers 3

To quote the Codex on the have_comments function:

This function relies upon the global $wp_query object to be set - this is usually the case from within The Loop

The problem is that your ajax handler creates its own WP_Query object. Note that you are not calling the_post(), instead you are calling $posti->the_post(). Same logic applies to comments as well.

Try the following:

if ($posti->have_comments()) {
    echo "Comments ok";
}  else {
    echo "No comments";
}
share|improve this answer
add comment

Look at the source of have_comments() - this check retrieves data from global $wp_query object, which is not used in your case.

So the first step would be to replace have_comments() check with $posti->have_comments().

share|improve this answer
add comment

On my opinion, it would be better to go with a JQuery .load($[this].attr('href') '.div-with-content-and-comment');

Than make sure you have a single.php that has the markup with the class="div-with-content-and-comment" you want to load via ajax.

share|improve this answer
add comment

protected by kaiser Jun 10 '13 at 1:02

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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