Make WordPress Core

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: presskopp's profile Presskopp 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)

warning.png (178.3 KB) - added by Presskopp 2 months ago.

Download all attachments as: .zip

Change History (16)

#1 @abcd95
2 months ago

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.

#2 @Presskopp
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 @dilipbheda
2 months ago

@Presskopp I can't reproduce the issue. When I visit a page that doesn't exist, it shows a 404 page.

#4 @Presskopp
2 months ago

Did you define('WP_DEBUG', true); ?

#5 @dilipbheda
2 months ago

@Presskopp Yes

#6 @Presskopp
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%/

Last edited 2 months ago by Presskopp (previous) (diff)

#7 @sabernhardt
2 months ago

  • Component changed from General to Feeds

(Line 3352 is inside the feed_links_extra() function)

#8 @Presskopp
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 @abcd95
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 @Presskopp
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)

Last edited 2 months ago by Presskopp (previous) (diff)

@Presskopp
2 months ago

#11 @Presskopp
2 months ago

  • Keywords has-screenshots added

#12 @Presskopp
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.

#13 @sabernhardt
2 months ago

  • Component changed from Feeds to General

#14 @indirabiswas27
2 months ago

I have carefully followed all the steps outlined, but I was only presented with the default 404 Not Found page, and no warnings appeared.

#15 @wordpressdotorg
5 weeks ago

  • Keywords needs-test-info added; needs-testing-info removed
Note: See TracTickets for help on using tickets.