Make WordPress Core

Opened 7 years ago

Closed 3 years ago

#42761 closed defect (bug) (worksforme)

Undefined offset: -1 in wp-includes/post-template.php

Reported by: twisted1919's profile twisted1919 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.1
Component: Posts, Post Types Keywords: reporter-feedback
Focuses: template Cc:



I am getting this notice:

<b>Notice</b>: Undefined offset: -1 in <b>/var/www/vhosts/domain/httpdocs/web/wp-includes/post-template.php</b> on line <b>287</b><br />

The logic that checks for index presence is faulty:

if ( $page > count( $pages ) ) // if the requested page doesn't exist
                $page = count( $pages ); // give them the highest numbered page that DOES exist

If $page is 0 or 1 and $pages is an empty array, you end up checking an out of bounds array and trigger this notice.

Maybe a more complete fix would be:

if ( $page > count( $pages ) ) // if the requested page doesn't exist
                $page = count( $pages ); // give them the highest numbered page that DOES exist

        $content = isset($pages[$page - 1]) ? $pages[$page - 1] : '';

Ow whatever you think it will avoid this.


Change History (3)

#1 @dd32
7 years ago

  • Component changed from General to Posts, Post Types
  • Focuses template added
  • Keywords reporter-feedback added

Hey @twisted1919 and welcome back to Trac.

Can you provide some extra details on how you run into this? In what situations is $pages an empty array for example?

For an empty post, $pages should be array( 0 => '' )

#2 @twisted1919
7 years ago


I am not exactly sure how to have you reproduce this, but it started when we upgraded woocommerce to latest version and then we also did this for wordpress, so i am not sure, maybe other plugins somehow interacts with that global variable, but eitherway, maybe wordpress should do a more in-depth check at that particular point to avoid this issue.

If you need anything else from me, list of plugins, or whatever info, let me know.


#3 @hellofromTonya
3 years ago

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

Hello @twisted1919,

Thank you for your report.

It sounds like a plugin changed the value of the global $pages variable. As it is a global variable, any code can change its value. It's designed to give access to themes, plugins, etc. With that access comes the potential for the value to be changed in unexpected ways, such as setting it to an empty array.

I'm closing this ticket as it does not appear to be a bug in WordPress Core itself.

I can understand the thinking of having core guard the value to prevent other code from resetting it to an empty array. However, the pages value is exposed by design. Changing it could cause problems for themes and plugins that use it.

Note: See TracTickets for help on using tickets.