WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#7080 closed defect (bug) (duplicate)

a new WP_Query created in the loop, whose the_post() method is called, messes around with wp_the_query - "recent entries" widget is still loop-breaking

Reported by: msi08 Owned by: Denis-de-Bernardy
Milestone: Priority: normal
Severity: normal Version: 2.7
Component: Query Keywords: needs-patch
Focuses: Cc:

Description

I'm using a "related posts" plugin and I noticed a problem with the ID of the current post when the "recent entries" widget is used as the first widget. I found a solution somewhere in the internet but I'm surprised that it's still not a part of the trunk.

I don't know who to credit for (maybe Google can help), but here is the fix, I found and use.

Attachments (1)

widgets.patch (638 bytes) - added by msi08 6 years ago.

Download all attachments as: .zip

Change History (21)

msi086 years ago

comment:1 Denis-de-Bernardy5 years ago

  • Component changed from General to Widgets
  • Keywords has-patch 2nd-opinion added
  • Milestone changed from 2.9 to 2.8
  • Owner anonymous deleted
  • Version set to 2.7

comment:2 Denis-de-Bernardy5 years ago

  • Keywords needs-patch added; has-patch removed
  • Milestone changed from 2.8 to Future Release

broken patch

comment:3 Denis-de-Bernardy5 years ago

  • Milestone Future Release deleted
  • Resolution set to invalid
  • Status changed from new to closed

invalid, given how wp_reset_query() behaves

comment:4 Denis-de-Bernardy5 years ago

  • Milestone set to 2.8
  • Resolution invalid deleted
  • Status changed from closed to reopened

annoyingly, I just ran into this one myself.

comment:5 Denis-de-Bernardy5 years ago

  • Keywords 2nd-opinion removed

patch doesn't help much either.

to reproduce, do the following in the loop:

the_title(); the post's title

insert widget here

the_content(); the content of the widget's last post

comment:6 Denis-de-Bernardy5 years ago

something in there is changing wp_the_query.

comment:7 Denis-de-Bernardy5 years ago

  • Owner set to Denis-de-Bernardy
  • Status changed from reopened to assigned

it's occurring around the_post(), but I can't figure out what it might be.

comment:8 Denis-de-Bernardy5 years ago

  • Component changed from Widgets to Query

comment:9 Denis-de-Bernardy5 years ago

  • Summary changed from "recent entries" widget is still loop-breaking to the_post() called on an arbitrary query messes around with wp_the_query - "recent entries" widget is still loop-breaking

comment:10 Denis-de-Bernardy5 years ago

  • Summary changed from the_post() called on an arbitrary query messes around with wp_the_query - "recent entries" widget is still loop-breaking to a new WP_Query created in the loop, whose the_post() method is called, messes around with wp_the_query - "recent entries" widget is still loop-breaking

comment:12 jacobsantos5 years ago

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

The Recent Posts widget already uses this technique (albeit in a less hackish way).

comment:13 jacobsantos5 years ago

If there is still a problem, then it is not where you expect it to be.

comment:14 jacobsantos5 years ago

Nothing is changing wp_the_query. When reset_query is run, then the wp_query is removed and then recreated with what is in wp_the_query. If you are running into problems, then it isn't in anything in the Widget but code outside of it.

comment:15 Denis-de-Bernardy5 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:16 Denis-de-Bernardy5 years ago

Jacob, I wouldn't have opened the ticket if it had occurred in trunk. ;-)

comment:17 Denis-de-Bernardy5 years ago

had not occurred, even. there is *major* bug in wp_reset_query atm. try the procedure I outline above and see for yourself. It *wrecks* everything. insert the recent posts before calling the_content(), you'll see the post's contents turned into the recent widget's last items' contents.

comment:18 Denis-de-Bernardy5 years ago

also, consider adding this after the call to the_post() in the recent posts widget:

var_dump($GLOBALS['wp_the_query']->post->ID);

you will notice that it changes.

comment:19 Denis-de-Bernardy5 years ago

  • Resolution set to duplicate
  • Status changed from reopened to closed

merging this into #9854

comment:20 Denis-de-Bernardy5 years ago

  • Milestone 2.8 deleted
Note: See TracTickets for help on using tickets.