Skip to content

PHP warning in wp comment recount command with invalid post ID #464

Closed

Description

@ernilambar

Bug Report

Describe the current, buggy behavior

In comment recount command, if Post ID does not exist, PHP warnings are generated.

wp comment recount 111

PHP Warning:  Attempt to read property "ID" on null in /Users/nilambarsharma/.../extension-command/vendor/wp-cli/entity-command/src/Comment_Command.php on line 680
Warning: Attempt to read property "ID" on null in /Users/nilambarsharma/.../extension-command/vendor/wp-cli/entity-command/src/Comment_Command.php on line 680
Warning: Post  doesn't exist.

Describe what you would expect as the correct outcome

Proper message should be displayed without PHP warnings.

Let us know what environment you are running this on

OS:     Darwin 22.6.0 Darwin Kernel Version 22.6.0: Tue Nov  7 21:48:06 PST 2023; root:xnu-8796.141.3.702.9~2/RELEASE_X86_64 x86_64
Shell:  /bin/zsh
PHP binary:     /usr/local/Cellar/php@8.2/8.2.15/bin/php
PHP version:    8.2.15
php.ini used:   /usr/local/etc/php/8.2/php.ini
MySQL binary:   /usr/local/bin/mysql
MySQL version:  mysql  Ver 8.3.0 for macos13.6 on x86_64 (Homebrew)
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /Users/nilambarsharma/Sites/staging
WP-CLI packages dir:    /Users/nilambarsharma/.wp-cli/packages/
WP-CLI cache dir:       /Users/nilambarsharma/.wp-cli/cache
WP-CLI global config:   /Users/nilambarsharma/.wp-cli/config.yml
WP-CLI project config:  /Users/nilambarsharma/Sites/staging/wp-cli.local.yml
WP-CLI version: 2.10.0

Activity

  1. swissspidy commented on Feb 13, 2024

    @swissspidy
    Member

    Looks like it's just a matter of moving the function calls around here;

    foreach ( $args as $id ) {
    wp_update_comment_count( $id );
    $post = get_post( $id );
    if ( $post ) {
    WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." );
    } else {
    WP_CLI::warning( "Post {$post->ID} doesn't exist." );
    }
    }

    wp_update_comment_count() should be inside the if.

    Wanna submit a PR? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions