Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Fatal error: Uncaught Error: Typed property Timber\Post::$wp_object must not be accessed before initialization in /timber/timber/src/Post.php on line 297 #2955

Closed
pratik-londhe4 opened this issue Mar 19, 2024 · 2 comments

Comments

@pratik-londhe4
Copy link

Expected Behavior

No error should come and page should loadthe

Actual behavior

Fatal error is coming

Steps to reproduce behavior

Here is the code I am using that is causing the issue :

$_posts =  Timber::get_posts(
		array(
			'posts_per_page' => 15,
			'post_status'    => 'publish',
			'paged'          => $paged,
			'post__not_in' => array(617240),
			'post_type'      => array( 'guide-post', 'post' ),
			'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery
				array(
					'taxonomy' => 'category',
					'field'    => 'slug',
					'terms'    => array( 'updates' ),
					'operator' => 'NOT IN',
				),
			),
		)
	);


foreach ( $_posts as $recent_post ) {  //<-- this is where the error starts in stack trace
	if ( in_array( $recent_post->ID, $posts_to_exclude_ids ) ) {
		continue;
	} else {
		$i ++;
	}

if ( ( count( $latest_pinned_post ) + count( $latest_posts ) ) < $article_count ) {
		// Add only those posts which are showing in latest column.
		// Exclude posts for recent posts.
		$exclude_global_front_posts[] = $recent_post->ID;
	}
	$latest_posts[] = $recent_post;
}

$context['posts']                      = $_posts;

Notes

call stack :

Call stack:

    Timber\Post::setup()
    wp-content/themes/mytheme/vendor/timber/timber/src/PostsIterator.php:51
    Timber\PostsIterator::current()
    wp-content/themes/mytheme/front-page.php:89
    include()
    wp-includes/template-loader.php:106
    require_once()
    wp-blog-header.php:19
    require()
    index.php:17

this can be solved by assigning null to the $wp_object in Post.php

protected ?WP_Post $wp_object;

protected ?WP_Post $wp_object = null;

I found this reference for the solution: https://stackoverflow.com/questions/59265625/why-i-am-suddenly-getting-a-typed-property-must-not-be-accessed-before-initiali

What version of Timber are you using?

2

What version of WordPress are you using?

6.3.3

What version of PHP are you using?

8.1

How did you install Timber?

Installed or updated Timber through Composer

@Levdbas
Copy link
Member

Levdbas commented Mar 19, 2024

Hi @pratik-londhe4 ,

What happens if you add ->to_array() method at the end of your query.

$_posts =  Timber::get_posts(
		array(
			'posts_per_page' => 15,
			'post_status'    => 'publish',
			'paged'          => $paged,
			'post__not_in' => array(617240),
			'post_type'      => array( 'guide-post', 'post' ),
			'tax_query'      => array( // phpcs:ignore WordPress.DB.SlowDBQuery
				array(
					'taxonomy' => 'category',
					'field'    => 'slug',
					'terms'    => array( 'updates' ),
					'operator' => 'NOT IN',
				),
			),
		)
	)->to_array();

I wonder if that fixes your particular issue.

Might be related to #2917?

@pratik-londhe4
Copy link
Author

hi, @Levdbas Yes, using to_array() fixes the issue. Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants