Make WordPress Core

Opened 8 years ago

Closed 7 years ago

#39257 closed defect (bug) (invalid)

admin_notices bug when using wp_query

Reported by: garethgillman's profile garethgillman Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: Administration Keywords:
Focuses: administration Cc:


I have the following code:

This causes:

It auto fills out the title, slug and content from the last post in the query, tested on 2 different cleans sites on 4.5 and 4.6

It leads me to believe that the global $query object is being interfered with by instantiating a new WP_Query class inside the admin_notices hook

Change History (2)

This ticket was mentioned in Slack in #core by garethgillman. View the logs.

7 years ago

#2 @johnbillion
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi @garethgillman.

The bug you're seeing is caused by the $all_tasks_query->the_post() line. This sets the global $post object to the current post in the loop. This same object is used to determine whether to populate the fields on the post editing screen. In addition, the call to wp_reset_postdata() has no effect because there is no post data to reset to.

the_post() method is not intended to be used in the admin area in this way, and may have side effects on other screens too. You should refactor your code to use the results of the WP_Query call directly, instead of calling $all_tasks_query->the_post().

Note: See TracTickets for help on using tickets.