Support » Fixing WordPress » Multiple Loop weirding out my comments!

  • OK… kinda a noob here, but working on coding my site and I’m trying to use a loop inside the main loop on the single.php page. It basically runs a query for a certain number of posts with a certain tag. If it finds posts, then it displays a link (in the form of an image pulled from a custom field) to the post(s). This code runs just before I call the comments template.

    Here’s the basic skeleton:

    <?php $premium_Posts = new WP_query('tag=premium&showposts=6') ;
    while ($premium_Posts->have_posts()) : $premium_Posts->the_post(); ?>
    <!-- Do all my custom stuff inside here -->
    <?php endwhile; ?>

    This all works fine, and displays as needed. BUT — if this loop finds posts, then when I make a comment on any page, it shows up on the “premium” post and this comment also appears on ALL my posts — even making other comments disappear.

    However, if this loop does NOT find posts, the comments seem to work as expected. It seems like the comments are somehow being posted within this custom loop, but I have my “endwhile” statement before the comments template is called.

    Can someone discern what might be happening here? Thanks so much!


Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m a noob too, but I had this problem myself and just figured it out. The post a comment belongs to is determined by the most recent post in the loop. On a normal page, no problem. There’s only 1 post to choose from.

    When you run another loop on that page, the comment thinks it belongs to the most recent post it saw, so it attaches itself to that page and that’s where you’re redirected to.

    Here’s what you’ve got (if I understand correctly):

    • Main Loop (the main one for the page)
    • Custom Loop (the custom query you created)
    • With “premium” being the last post displayed on the page.
    • Comments (which depend on the loop to determine where the comment belongs)

    Here’s how I fixed it:

    1. Start the loop and define a variable with the page/post ID. i.e. $thisPageId)
      if (have_posts()) : while (have_posts()) : the_post();
      $thisPageID = get_the_ID();
      $thisPageID = 'p='.$thisPageID;
    2. Run your custom loop & close it
    3. Run a third loop for that page/post ID before comments
      $recent = new wp_query($thisPageID);
      while($recent->have_posts()) :
      $recent->the_post(); ?>
    4. Include comments as usual
      <div class="comments">
      <?php comments_template('',true); ?>
    5. Close the “third” loop
      <?php  endwhile; ?>

    As for your issue with the comment posting on all posts and deleting other comments, I’m not sure about that.

    Maybe you included comments inside the custom loop somehow? Not sure…

    If you run the first loop, run the second loop, close both of them then run the third loop with comments inside, I think it will fix it all for you.

    Good luck.

    @jasonmassengale, I’m dealing with the same problem too. Your way works like a charm~ Thanks a lot!


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Multiple Loop weirding out my comments!’ is closed to new replies.