WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#10048 closed defect (bug) (invalid)

Condition tag is_home() returns wrong result if it is called after query_posts()

Reported by: kamiyeye Owned by:
Milestone: Priority: high
Severity: major Version: 2.8
Component: General Keywords:
Focuses: Cc:

Description

Pre-requisites:

Prepare a test-purpose php script, say:

$home = is_home() ? 'you are home': 'you are NOT home';
echo '<script type="text/javascript">alert("'.$home.'")</script>';

query_posts('showposts=1');
			
$home = is_home() ? 'you are home': 'you are NOT home';
echo '<script type="text/javascript">alert("'.$home.'")</script>';

Repro Steps

  1. Insert the test-purpose php script into a template php file, say single.php, then save.
  2. Visit any single post page in the blog

Expected Result

A message box pops up twice with the same text on it: you are NOT home

Actual Result

A message box pops up twice with differen texts on it, first is "you are NOT home", second is "you are home"

Change History (2)

comment:1 @dd326 years ago

Yes, Using query_posts() overrides the current global loop.

AFAIK you should use a custom loop (ie. new WP_Query) instead if you wish to retain THE loop whilst having your own loop.

See also: #9854

comment:2 @ryan6 years ago

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

wp_reset_query() is also handy.

Note: See TracTickets for help on using tickets.