WordPress – the comments bug has been fixed

There was a very annoying bug. On each of post pages the Disqus plugin, which is responsible for loading comments section, used to show same comments for all of the posts. It took me a few hours of debugging to find out where the issue was.

In March 2017 I added the Popular Posts box. It is generated with following code

The Disqus plugin showed comments for the last post that was generated by the above loop. That gave me a hint to check current post’s id with the get_the_ID method.

At that moment everything was clear. The “Popular posts” loop overrode post’s id. The solution was fairly easy, I had to reset the data. Fortunately, WordPress provides a dedicated function called wp_reset_postdata. The following one-liner was a solution for the bug on which I spend, more or less, 6 hours of debugging and reinstalling the Disqus plugin:

There is another method called wp_reset_query. It should be used only when we use the query_post method. For more information about differences between those methods please visit this post.

To sum everything up, I think that solving this problem took me so much time because I’m not familiar with WordPress conventions. The most important thing was to connect the “Popular posts” box with the plugin problems.

You may also like