Skip to content

PHP warning about comment_count on null #76

@cagrimmett

Description

@cagrimmett

I'm getting this recurring warning on my WP site from jsonfeed:

PHP Warning: Attempt to read property "comment_count" on null in /srv/htdocs/wp-content/plugins/jsonfeed/jsonfeed-wp.php on line 93

The relevant code block:

89 if ( is_singular() ) {
90		$id       = 0;
91		$post     = get_post( $id );
92		$comments = apply_filters( 'jsonfeed_comments_feed_enable', true );
93		if ( $comments && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
94			$title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
95			$href  = get_post_comments_feed_link( $post->ID, 'json' );
96		}
97	} elseif

I think the issue is that since the $id is initally set to 0 and I don't have a post ID 0 in my database, that line is trying to access comment_count on null.

This failed silently in PHP 7.x, but PHP 8.x has stricter property access rules, so it throws a warning.

This can be fixed by doing a null check on $post in line 93:

if ( $post && $comments && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {

I'll open up a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions