[Plugin: WP Realtime Sitemap] Leaves global $post variable in flawed state
-
I just realized that this sitemap plugin leaves the global $posts menu in a flawed state because it launches new WP “loops”. To see this problem, put the wp-realtime-sitemap shortcode on a page that allows comments. Notice that the comments will be those of the last post and not those of the page on which the shortcode sits.
I had to do a couple things to fix this. First, I removed all instances of
wp_reset_postdata()
from the script. This function seems to be unnecessary, and when called it messes up the outer WP loop. Second I added lines to save and restore the state of the global $post variable around each inner WP loop.if ($all_options['show_posts'] != 'no' && $all_options['show_posts'] != 'off') { $current_post = $post; // save the current post so we can restore state later ... $post = $current_post; // reset the post from the main loop }
…and…
if ($all_options['show_custom_post_types'] != 'no' && $all_options['show_custom_post_types'] != 'off') { $current_post = $post; // save the current post so we can restore state later ... $post = $current_post; // reset the post from the main loop }
This seems to resolve the problem. Maybe it could be rolled into the next version?
This fix was based on the article A Loop Inside A Loop.
…Eric
- The topic ‘[Plugin: WP Realtime Sitemap] Leaves global $post variable in flawed state’ is closed to new replies.