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.

After commenting on one of my posts, WordPress redirects me to another random post. Only after, when checking the previous post, I can see that my comment was posted. Still, why is WordPress sending me to a random post after commenting?

Here's my comments.php file and here's my website (please don't publish it anywhere else, as I'm launching it tomorrow). Feel free to test the comments, though. I'll clear then before I launch it. Any suggestions?

PS: I'm using no plugins and the bug doesn't work on other themes. Also, I'm using pretty /%postname%/ permalinks.

share|improve this question
    
Just tried it, and I was sent to the right post after commenting. Can you give us a step-by-step list of what you did so we can try to recreate the problem? –  EAMann Jun 7 '11 at 21:03
    
Just solved my problem, using wp_reset_query(); :) –  João Ramos Jun 8 '11 at 10:41
add comment

2 Answers

up vote 3 down vote accepted

You're doing something that is affecting the value of the main $post variable in an incorrect manner.

The WordPress template consists of one main Loop. That Loop, on a single post page, shows the one and only Post (or Page).

When the comments form runs, it expects the last post from the Loop to be the one you're commenting on. However, if you mess with the main Loop after the fact (like by doing another query_posts() or altering the global $post variable in any way), then you'll muck that bit up. This is what is happening on your posts.

If you look at the source of http://joaoramos.org/sala-de-ser/ you'll see that that post's ID number is 635, but the comments form thinks it's 630. I'm surprised the comments go to the right pages.

Specifically, what is happening here is that your sidebar is doing-it-wrong™. If you'll notice, the last entry in your sidebar is for http://joaoramos.org/via/ which, BTW, is post number 630.

When you make secondary Loops, you should create new WP_Query objects instead of modifying the main one and your loop shouldn't modify any global variables, if possible.

No quick fix to this one. Rewrite your sidebar to not muck up the main Loop.

Edit: Didn't know about this one. Just add a call to wp_reset_postdata() after the sidebar loops run to fix up the global $post information back to what it should be.

share|improve this answer
    
That's it, Otto. Thank you so much! In fact, it worked fine after commenting the sidebar include. And, actually, I'm using two different WP_Query objects on my sidebar. Do you know how to stop them for messing the main loop? Here's my sidebar file, just in case. –  João Ramos Jun 8 '11 at 7:13
    
Add a call to wp_reset_postdata(); after your sidebar Loops run. This will fix up the global $post back to what it should be. –  Otto Jun 8 '11 at 16:59
    
Why wp_reset_postdata(); instead of wp_reset_query();? –  João Ramos Jun 8 '11 at 17:07
    
Resetting the query resets the main query object if you happened to override it, which using new WP_Query doesn't do. Your problem is the use of $yourquery->the_post(), which upsets the main post object. So you need to reset the main post object from the default query, which is what wp_reset_postdata() does. –  Otto Jun 8 '11 at 17:10
add comment

The redirect after comment being processed is performed by following lines of code in wp-cmments-post.php:

$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;
$location = apply_filters('comment_post_redirect', $location, $comment);

wp_redirect($location);

So there are three ways this can go wrong:

  • ID for comment that was just created is incorrect;
  • submitted form data contains custom redirect location;
  • location is filtered and changed.

From information you provided it isn't really possible to guess which one of these might be at fault.

share|improve this answer
add comment

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.