WordPress

Notice: Object of class WP_Post could not be converted to int in /app/public/wp-includes/general-template.php


I faced this error while working on a project and it wasted some of my time to figure out why it was happening. I had the following code.

$pages = get_pages();

if( is_array( $pages ) && count( $pages ) > 0 ) {
	foreach ( $pages as $page ) {
		// Some stuff I am planning to do
		}
	}
}

It gave me the following error and it was not clear about its location which makes it a bit harder to figure out when I was not working on the file having that code done a while ago.

Notice: Object of class WP_Post could not be converted to int in /app/public/wp-includes/general-template.php on line 1098

It looks like the variable name $page is reserved. Anyways, I changed the code to the following which fixed the issue.

$all_pages = get_pages();

if( is_array( $all_pages ) && count( $all_pages ) > 0 ) {
foreach ( $all_pages as $single_page ) {
// Some stuff I am planning to do
}
}
}


WordPress
Remove Category or Tag Slug from url
Quicky
Fixing Notice: Function _load_textdomain_just_in_time was called incorrectly for WordPress 6.7 update
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Hassan Raza