Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions features/db-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,31 @@ Feature: Search through the database
"""
And STDERR should be empty

Scenario: Search with exclude revisions option
And I run `wp post create --post_content="This is the original post content." --post_title="Original Post" --porcelain`
Then save STDOUT as {POST_ID}
# Create a revision
And I run `wp post update {POST_ID} --post_content="This is the updated post content."`
When I run `wp post list --post_parent={POST_ID} --post_type=revision --field=ID`
Then save STDOUT as {REVISION_ID}

When I run `wp db search "updated post content"`
Then STDOUT should contain:
"""
wp_posts:post_content
{POST_ID}:This is the updated post content.
wp_posts:post_content
{REVISION_ID}:This is the updated post content.
"""

When I run `wp db search "updated post content" --exclude_revisions`
Then STDOUT should contain:
"""
wp_posts:post_content
{POST_ID}:This is the updated post content.
"""
And STDERR should be empty

Scenario: Search with custom colors
Given a WP install

Expand Down
17 changes: 15 additions & 2 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,9 @@ public function prefix() {
* - count
* ---
*
* [--exclude_revisions]
* : Exclude revisions from the search.
*
* The percent color codes available are:
*
* | Code | Color
Expand Down Expand Up @@ -1490,6 +1493,7 @@ public function search( $args, $assoc_args ) {
$stats = Utils\get_flag_value( $assoc_args, 'stats', false );
$fields = Utils\get_flag_value( $assoc_args, 'fields' );
$format = Utils\get_flag_value( $assoc_args, 'format' );
$exclude_revisions = Utils\get_flag_value( $assoc_args, 'exclude_revisions', false );

$column_count = 0;
$row_count = 0;
Expand Down Expand Up @@ -1554,10 +1558,19 @@ public function search( $args, $assoc_args ) {
}

foreach ( $text_columns as $column ) {
$column_sql = self::esc_sql_ident( $column );
$column_sql = self::esc_sql_ident( $column );
$post_type_sql = self::esc_sql_ident( 'post_type' );
if ( $regex ) {
Comment on lines 1560 to 1563
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$post_type_sql is computed for every text column, but it never changes and is only needed when $exclude_revisions applies. Moving this identifier escaping outside the inner $text_columns loop (or only computing it when needed) would reduce repeated work and make the control flow easier to follow.

Copilot uses AI. Check for mistakes.
if ( $exclude_revisions && $wpdb->posts === $table ) {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$post_type_sql} NOT IN ( 'revision' )" );
} else {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" );
}
} elseif ( $exclude_revisions && $wpdb->posts === $table ) {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" );
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s AND {$post_type_sql} NOT IN ( 'revision' )", $esc_like_search ) );
Comment on lines +1564 to +1573
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--exclude_revisions is only applied when $table === $wpdb->posts, which means revisions will still appear when searching a different posts table name (e.g. wp_2_posts during --network/--all-tables-with-prefix, or when the user explicitly passes another posts table). Consider applying the filter based on the table being a posts table (e.g. suffix check + verifying a post_type column exists), rather than only the current site's $wpdb->posts value.

Copilot uses AI. Check for mistakes.
} else {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like.
$results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s;", $esc_like_search ) );
Expand Down
Loading