Opened 2 months ago
Last modified 5 weeks ago
#63263 new defect (bug)
check if exists before accessing object properties of $post->comment_count
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | needs-test-info has-screenshots |
Focuses: | Cc: |
Description
I got a
Warning: Attempt to read property "comment_count" on null in [...]\wp-includes\general-template.php on line 3352
let's check if the object exists and the property is not empty.
Original line:
if ( $show_post_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
possible patch, something like:
if ( $show_post_comments_feed && isset( $post ) && $post instanceof WP_Post && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
Attachments (1)
Change History (16)
#2
@
2 months ago
Hi @abcd95
I only get the warning when I provoke it. This happens during testing, not in productive operation. Nevertheless, I think the line should be changed.
You can provoke it by opening the Posts page and add a non existing page query like ?p=12345 to it.
Sorry, I should have mentioned it.
#3
@
2 months ago
@Presskopp I can't reproduce the issue. When I visit a page that doesn't exist, it shows a 404 page.
#6
@
2 months ago
Interesting. I tried with Twenty Twenty-Five and Twenty Seventeen and I see the warning in both cases. Maybe it's because you have no posts at all or something!?
Please set permalinks to /%postname%/
#7
@
2 months ago
- Component changed from General to Feeds
(Line 3352 is inside the feed_links_extra()
function)
#8
@
2 months ago
FYI: there's a function get_comments_number( $post = 0 )
, which could be helpful here. Also there are more instances of $post->comment_count
in 3 more core files.
#9
@
2 months ago
- Keywords needs-testing-info added
@Presskopp I followed everything you said, and I still don't see any warnings anywhere.
I did the following things -
Ensured I have enough posts in the system
Changed permalinks to /%postname%/
Accessed an invalid post ID (like localhost/blog/?p=12345 and even localhost/?p=12345)
However, I was only able to see the standard 404 Not Found
page without any PHP warnings appearing. This matches the experience reported by @dilipbheda earlier.
Is there anything more to be done?
#10
@
2 months ago
Weird. All I do after a fresh installation is
- define('WP_DEBUG', true);
- change permalinks to anything but 'Plain'
- set a Posts page
- open that Posts page and add a non existing page parameter like ?p=12345.
- reload the page
- see the warning
It makes no difference which theme is active (talking about standard themes)
#12
@
2 months ago
As you can see on the screenshot, there's also an issue with the property "ID" using the Twenty Ten Theme. So we could open the scope of this ticket to a general check if objects and their properties exists, before assigning a value.
Hi, @Presskopp. Thanks for raising this.
Can you please sketch out the steps to reproduce the warning that you are observing?
Additional information, like the WP version or the theme version that you are using, will be helpful.