#59014 closed defect (bug) (worksforme)
PHP Fatal error in post-template.php
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | minor | Version: | 6.3 |
| Component: | Posts, Post Types | Keywords: | needs-patch |
| Focuses: | template | Cc: |
Description (last modified by )
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)
#2
@
3 months ago
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
#3
in reply to:
↑ description
@
4 weeks 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' );
Reproduction Report
Description
❌ This report can't validate that the issue can be reproduced.
Environment
Testing Instructions
<?php the_content(); ?>to the templateActual Results
Additional Notes