Make WordPress Core

Opened 3 years ago

Closed 12 months ago

Last modified 9 months ago

#59014 closed defect (bug) (worksforme)

PHP Fatal error in post-template.php

Reported by: volqld Owned by:
Priority: normal Milestone:
Component: Posts, Post Types Version: 6.3
Severity: minor Keywords: needs-patch
Cc: Focuses: template

Description (last modified by sabernhardt)

Hi there,

I hope this email finds you well. Unfortunately, we've recently encountered a critical error that requires immediate attention. This issue pertains to the single page templates, specifically an example like this: https://volunteeringqld.org.au/governance/before-you-join/. The error was not present a week ago, and we're currently grappling to determine its cause. This problem was initially observed on both WP6.22 and WP6.3.

The post https://volunteeringqld.org.au/governance/before-you-join/ connected to a single template php mytemplate/single-governance-before-you-join.php, what calls <?php the_content() ?> what throws error "There has been a critical error in this website" and output steam finishes. Log error gives this line:

"PHP message: PHP Fatal error: Uncaught TypeError: Unsupported operand types: WP_Post - int in /.../wp-includes/post-template.php:330
Stack trace:
#0 /.../wp-includes/post-template.php(247): get_the_content()
#1 /.../wp-content/themes/volunteeringAU/single-governance-before-you-join.php(411): the_content()
#2 /.../wp-includes/template-loader.php(106): include('...')
#3 /.../wp-blog-header.php(19): require_once('...')
#4 /.../index.php(17): require('...')
#5 {main} thrown in /.../wp-includes/post-template.php on line 330', referer: https://volunteeringqld.org.au/governance/"

To address this, we have applied the following code snippet in post-template.php line 330:

	if( ! is_int($page_no)) { 
		$page_no = 1; // Igor
		//echo '<!--'.print_r( $elements, true ). '-->';
	}

When echo ancommented it gives

Array
(
    [page] => WP_Post Object
        (
            [ID] => 5
            [post_author] => 7
            [post_date] => 2021-10-21 04:24:08
            [post_date_gmt] => 2021-10-21 04:24:08
            [post_content] => 
            [post_title] => Home
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => home
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2023-05-26 13:50:03
            [post_modified_gmt] => 2023-05-26 03:50:03
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => https://volunteeringqld.org.au/?page_id=5
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

    [more] => 1
    [preview] => 
    [pages] => Array
        (
            [0] => <!-- wp:paragraph -->
<p><strong>Achieving a good transition to the next volunteer who will take over from you has benefits for you, the organisation, and the incoming governance member. Let’s explore things you can do to ensure a good handover.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this stage of your Governance journey we explore:</p>
<!-- /wp:paragraph -->
        )

    [multipage] => 0
)

As you can see, instead of having 1 in $elements['page'], it contains a WP_Post object of the very first post from the database, even not the one that is displayed.

We are hopeful that this information helps you in resolving the issue and that a solution will be included in an upcoming patch. Please don't hesitate to reach out if you require more details or assistance.

Best regards,
Igor

Change History (3)

#1 @sabernhardt
3 years ago

  • Component GeneralPosts, Post Types
  • Description modified (diff)

#2 @SirLouen
12 months ago

  • Milestone Awaiting Review
  • Resolutionworksforme
  • Status newclosed

Reproduction Report

Description

❌ This report can't validate that the issue can be reproduced.

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.29
  • Server: nginx/1.29.1
  • Database: mysqli (Server: 8.4.6 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 139.0.0.0
  • OS: Windows 10/11
  • Theme: Minimal Theme 1.0.0
  • MU Plugins: None activated
  • Plugins:
    • BBB Testing Dolly
    • Test Reports 1.2.0

Testing Instructions

  1. Use a non block theme like 2020, or create your own theme (I'm using my own)
  2. Create a post type, lets call it like in OP, Governance
  3. Create a single template for such CPT.
  4. Only add <?php the_content(); ?> to the template
  5. Check a post within such template (in this case example.com/governance/post-name)
  6. 👌 The post is shown correctly, no errors being triggered

Actual Results

  1. ❌ Error condition is not occurring

Additional Notes

  • There might be more elements conflicting. This error, by default, is not reproducible.
Last edited 12 months ago by SirLouen (previous) (diff)

#3 in reply to: ↑ description @nuagelab
9 months ago

The cause of this like likely that you are using the variable $page somewhere in the template of the page causing the error.

This is simple to reproduce. Just take a WordPress template (ex. page.php)

Set it to this:

<?php
   get_header();

   $page = $post;
   the_content();

   get_footer();
?>

It is likely due to this line in post-template.php around line 293:

$elements = compact( 'page', 'more', 'preview', 'pages', 'multipage' );
Note: See TracTickets for help on using tickets.