[wp-trac] [WordPress Trac] #63263: check if exists before accessing object properties of $post->comment_count

WordPress Trac noreply at wordpress.org
Wed Aug 27 18:46:33 UTC 2025


#63263: check if exists before accessing object properties of $post->comment_count
---------------------------------------------+-----------------------------
 Reporter:  Presskopp                        |       Owner:  (none)
     Type:  defect (bug)                     |      Status:  new
 Priority:  normal                           |   Milestone:  Awaiting
                                             |  Review
Component:  General                          |     Version:
 Severity:  normal                           |  Resolution:
 Keywords:  needs-test-info has-screenshots  |     Focuses:
---------------------------------------------+-----------------------------

Comment (by awetz583):

 For now, I can add this to our theme as a workaround to remove the
 warning:


 {{{#!php
 <?php
 add_action( 'template_redirect', function() {
     if ( is_404() ) {
         add_filter( 'feed_links_extra_show_post_comments_feed',
 '__return_false' );
     }
 });
 }}}



 Replying to [comment:16 awetz583]:
 > I see this error as well on the custom classic/hybrid theme I'm working
 on.
 >
 > To reproduce I:
 >
 > 1. Go to a page that really does result in a 404 (like
 `/nopenopenopenope/thisnomatch` so it doesn't attempt to redirect to
 another post in the DB)
 > 2. The Page loads fine, but I see the PHP warning `Attempt to read
 property "comment_count" on null`
 >
 > I think this is because there is now way to get a comment_count on a 404
 page.
 > `if ( $show_post_comments_feed && ( comments_open() || pings_open() ||
 $post->comment_count > 0 ) ) { ...`
 >
 > **Here is the error stack trace for WP 6.8.2:**
 >
 > wp-includes/general-template.php:3352
 >
 > feed_links_extra()
 > wp-includes/class-wp-hook.php:324
 >
 > do_action('wp_head')
 > wp-includes/general-template.php:3192
 >
 > wp_head()
 > wp-content/themes/my-theme/header.php:7

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63263#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list