• Resolved Eric

    (@eceleste)


    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Rincewind

    (@rincewind)

    Hi eceleste,

    Its been like this since v1 of the plugin and I have been trying to fix it ever since then, I have posted on the wp-hackers mailing list about the issue and how to fix it in my plugin, with no response from anyone, I have posted on the forums also about it, and also no response from anyone.

    I have tried to use get_posts instead of the query_posts which is the development version available to download if you click on other versions on the plugin website page, but this page does what you said it gives me comments and the comment form in my test site, but I am confused as to why as I have disabled comments for the page that I am using for the sitemap.

    I mistakenly got this confused with a bug so I posted on the wp-testers mailing list and was told on there initially to use the method you have mentioned, but then someone else replied and advised against doing this and to use this function instead wp_reset_postdata() which I tried but alas this hasn’t fixed my issue with the get_posts method. You can see the example code that I was told to use here as suggested by the people from wp-testers mailing list http://wordpress.pastebin.com/srRYBqPF

    I looked at the page that you have linked and that seems to suggest to put the code in with the foreach loop, I have tried this and this seems to work great for me in my development version I am not sure if you would like to download this.

    You seem to be already using the development version as I released it and then realised the issue and rolled back the stable tag to the previous version, care to test this for me ?

    Thread Starter Eric

    (@eceleste)

    I know nothing about wp_reset_postdata(), but since you say this was advised, I looked into it a bit. There is a bit of a definition at:

    http://phpxref.ftwr.co.uk/wordpress/wp-includes/query.php.html#wp_reset_postdata

    Here it says that the function must be used _after_ going through your inner loop.

    Notice that in the pastebin example at:

    http://wordpress.pastebin.com/srRYBqPF

    The wp_reset_postdata() is done at the end of the function. That said, I can’t seem to make this work in your plugin even with the call at the end, so I really don’t understand this function and what it does.

    For me, the explicit saving of the $post variable and resetting it each time does the trick. You should maybe give this method a try. I have attached the full source code. See if it works for you.

    …Eric

    [Moderator note: snipped code block, please use the pastebin as per the Forum Guidelines]

    Plugin Author Rincewind

    (@rincewind)

    I have tested it and it seemed to work ok for me, have released it now in both v1.4.8 and v1.5, give it a go and see how it works for you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: WP Realtime Sitemap] Leaves global $post variable in flawed state’ is closed to new replies.