Make WordPress Core

Opened 9 years ago

Closed 7 years ago

Last modified 7 years ago

#37312 closed defect (bug) (fixed)

Invalid call to wp_reset_postdata inside Recent Posts widget

Reported by: peterwilsoncc's profile peterwilsoncc Owned by: westonruter's profile westonruter
Milestone: 4.9 Priority: normal
Severity: normal Version: 3.0
Component: Widgets Keywords: has-patch commit
Focuses: Cc:

Description

If the global post object is from a query other than the main query, the recent posts widget will restore the wrong post object by calling wp_reset_postdata.

$my_query = new WP_Query( $args );
$my_query->the_post(); 
// $GLOBALS['post'] is first post of $my_query
the_widget( 'WP_Widget_Recent_Posts' );
// $GLOBALS['post'] is restored to the main query's last accessed post

Introduced in [14607] following #12320.

Attachments (2)

37312.diff (1.4 KB) - added by welcher 7 years ago.
Changing to foreach
37312.1.diff (2.2 KB) - added by westonruter 7 years ago.
Add missing printing of permalink; bring code up to par with WPCS

Download all attachments as: .zip

Change History (11)

@welcher
7 years ago

Changing to foreach

#1 @welcher
7 years ago

  • Keywords has-patch added

Changing the loop to a foreach negates the need to call the_post() or wp_reset_postdata.

#2 @welcher
7 years ago

  • Milestone changed from Awaiting Review to 4.9

#3 @westonruter
7 years ago

  • Owner set to westonruter
  • Status changed from new to reviewing

#4 @westonruter
7 years ago

  • Keywords commit added
  • Version set to 3.0

@westonruter
7 years ago

Add missing printing of permalink; bring code up to par with WPCS

#5 @westonruter
7 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 41890:

Widgets: Use a foreach loop instead of The Loop to iterate over posts in Recent Posts widget to avoid needing to reset a polluted global $post.

Props welcher, westonruter.
Amends [14607].
See #12320.
Fixes #37312.

#6 @superpoincare
7 years ago

This change has caused an issue for the plugin Jquery Archives List Widget:

https://plugins.trac.wordpress.org/browser/jquery-archive-list-widget/trunk/jquery-archive-list-widget.php

I have notified the author of that plugin but I also want to make sure that this change doesn't have bugs.

Reasonably sure that this change causes change in the behaviour of that plugin.

Can someone confirm that this ticket's changes hasn't caused unexpected behaviour?

#7 @westonruter
7 years ago

@superpoincare what is the issue?

#8 @superpoincare
7 years ago

Earier, the code for "Recent Posts" had wp_reset_postdata().

Now not. I understand this fix removes wp_reset_postdata() from the code but shouldn't it be the case that it should run after the html for Recent Posts has been created?

It seems that if I want to do something, in Widgets, I have to first use wp_reset_postdata(). Is that logical?

#9 @westonruter
7 years ago

If the jQuery Archive List Widget was relying on a global $post to be set then it was not correct as this is not reliable. It should instead be checking if is_singular() and if so, then grabbing the post ID from get_queried_object_id().

Note: See TracTickets for help on using tickets.