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
}
}
}
Comments