Skip to content

Fix false "SITE IS DISCONNECTED" error on empty query results - #1966

Open
anamwp wants to merge 2 commits into
xwp:developfrom
anamwp:fix/stream-query-empty-result-disconnected
Open

Fix false "SITE IS DISCONNECTED" error on empty query results#1966
anamwp wants to merge 2 commits into
xwp:developfrom
anamwp:fix/stream-query-empty-result-disconnected

Conversation

@anamwp

@anamwp anamwp commented Aug 13, 2026

Copy link
Copy Markdown

Fixes #1829

Problem

wp stream query runs a connectivity check (CLI::connection()) before executing, by running a real query for 1 record and treating a falsy result as "disconnected":

$query = wp_stream_get_instance()->db->query( [ ... ] );

if ( ! $query ) {
    \WP_CLI::error( 'SITE IS DISCONNECTED' );
}

DB::get_records() returns a plain array() when the query runs successfully but matches zero rows. In PHP, an empty array is falsy, so "no records yet" and "the query actually failed" were indistinguishable — any site with an empty wp_stream table (a fresh install, or a multisite subsite with no site-level activity logged) hit SITE IS DISCONNECTED even though nothing was actually wrong.

This matches the report in #1829: one multisite install logs plenty of activity and works fine, while a second logs almost nothing (only network-level actions) and fails on every wp stream query call.

Fix

Check $wpdb->last_error instead of the query's return shape. WordPress only populates last_error when a query genuinely fails at the database level (bad SQL, missing table, dropped connection, etc.), never for a query that simply matched nothing. This restores the original intent of the check — "is the database actually reachable" — without misreading an empty-but-healthy result as a failure.

Testing

  • Reproduced the bug locally: network-activated Stream on a multisite install, created a fresh subsite with zero logged activity, ran wp stream query --url=<subsite>SITE IS DISCONNECTED (confirmed the root cause, not just multisite-specific — a single-site install with an empty wp_stream table hits the same bug).
  • Applied the fix, re-ran the same commands against the root site and two empty subsites — all now return a clean, empty results table instead of erroring.
  • Verified the check still fires correctly on a genuine failure: temporarily renamed away the wp_stream table and confirmed SITE IS DISCONNECTED is still reported, then restored the table.
  • Full PHP test suite: 392 passing (pre-existing skipped/incomplete tests unrelated to this change).
  • composer lint (PHPCS): clean.
Screenshot 2026-08-13 at 7 45 21 AM

Test_CLI coverage added for the fix (both the empty-result and genuine-failure paths):

Screenshot 2026-08-13 at 9 00 56 AM

Checklist

  • Project documentation has been updated to reflect the changes in this pull request, if applicable. (N/A — internal WP-CLI logic fix, no user-facing docs affected)
  • I have tested the changes in the local development environment (see contributing.md).
  • I have added phpunit tests.

Release Changelog

  • Fix: wp stream query no longer reports "SITE IS DISCONNECTED" when a site simply has no logged activity yet.

anamwp added 2 commits August 13, 2026 08:05
wp stream query treated any empty result set as a disconnected site,
since db->query() returns array() for both "no matching records" and
implicitly for failures. This misfired on any site with no logged
activity yet (e.g. a fresh install or an untouched multisite subsite),
as reported in xwp#1829.

Check $wpdb->last_error instead, which WordPress only populates on an
actual database error, so a legitimately empty result no longer trips
the disconnected check.
Covers both branches of the fix from the previous commit: a query that
legitimately matches zero records should not trip WP_CLI::error(),
while a genuine database failure still should.

Uses WP_CLI's capture_exit mechanism (via reflection, since it's
private) to catch the resulting ExitException instead of terminating
the test process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: SITE IS DISCONNECTED on one multisite install. Works on another on same server.

1 participant