Make WordPress Core

Opened 10 years ago

Closed 4 years ago

#30273 closed defect (bug) (duplicate)

WP_Query with admin_bar_menu resets $post

Reported by: jag1989's profile jag1989 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: Query Keywords:
Focuses: administration Cc:

Description

I've been creating a new plugin based that adds some pages to the admin menu using the admin_bar_menu action. When using this action with a new WP_Query the global $post is being reset.

I've been able to work around this for now by temp setting and resetting the $post variable.

This certainly seems like a bug, as I see no reason to not make a new WP_Query in the admin bar.

                global $post;
		$temp_p = $post;

		$args = array(
			'post_type' => 'page',
			'post_status' => array(
				'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'
			),
			'posts_per_page' => -1,
		);
		$listpages = new WP_Query( $args );

		if ( $listpages->have_posts() ) :

			$args = array(
				'id'    => 'qn_pages',
				'title' => 'Jump to Page',
				'meta'  => array(
					'class' => 'wp-qn-container'
				)
			);
			$wp_admin_bar->add_node( $args );

			while ( $listpages->have_posts() ) : $listpages->the_post();
				$args = array(
					'id'    => get_the_ID(),
					'title' => get_the_title(),
					'href'  => get_edit_post_link(),
					'parent' => 'qn_pages'
				);
				$wp_admin_bar->add_node( $args );
			endwhile;
		endif;

		wp_reset_postdata();
		$post = $temp_p;

Change History (7)

#1 @boonebgorges
10 years ago

  • Component changed from General to Query
  • Keywords reporter-feedback added

jag1989 - Thanks for the report. Have you tested this against a recent version of WP trunk? There have been some changes since 4.0 that have improved the independence of secondary WP_Query objects. (See especially [30085].) Could you test against trunk to see if your problem is arising there as well?

Can you also clarify whether this is happening everywhere in your installation, or just on the Dashboard, or something else?

#2 follow-up: @jag1989
10 years ago

I have just checked out the latest truck and the same issue still occurs.

Issue happens only in the back-end on the following pages:
Edit/New Post
Edit/New Page

Editor/Slug/Title etc are all populated with the same content from another post. I this case ID 43 (not the lowest ID, either).

#3 in reply to: ↑ 2 @boonebgorges
10 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to Future Release

Replying to jag1989:

I have just checked out the latest truck and the same issue still occurs.

Issue happens only in the back-end on the following pages:
Edit/New Post
Edit/New Page

Ah, thanks for these additional details. It's happening on these pages and not others because the $post global on post.php is not populated by a WP_Query (which cleans up after itself). We'll need to introduce some additional logic into WP_Query to stash the content of the global before replacing it, and then put it back after the loop is finished. This logic already exists for nested queries (inside of reset_postdata()) but it doesn't work when $post has been manually set, as on post.php.

#4 @jag1989
10 years ago

Perfect, great explanation.

#5 @chriscct7
9 years ago

  • Keywords early added

#6 @chriscct7
9 years ago

  • Keywords early removed

Actually on second thought this probably doesn't need to be early.

#7 @Howdy_McGee
4 years ago

  • Keywords needs-patch removed
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #18408.

Note: See TracTickets for help on using tickets.