Skip to content

Commit 910738f

Browse files
committed
Improve WordPress.DB.DirectDatabaseQuery documentation
- Rewrite standard descriptions to explain why each rule exists. - Add <em> tags to highlight key parts in code examples. - Use realistic code examples. - Replace second caching example with a write + cache invalidation pattern. - Wrap caching examples in functions, as the sniff always generates a NoCaching warning if the caching code is not inside a function. - Remove dbDelta() recommendation from SchemaChange section. I believe the original intent of the rule is to discourage schema changes and not suggest dbDelta() is used. - Remove SchemaChange code comparison (no valid alternative).
1 parent b4901f6 commit 910738f

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

WordPress/Docs/DB/DirectDatabaseQueryStandard.xml

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,115 +5,105 @@
55
>
66
<standard>
77
<![CDATA[
8-
You should not make direct database queries, but instead use abstractions such as WP_Query.
8+
Direct database queries using $wpdb methods should be avoided. WordPress database abstraction layers handle caching, data integrity, and firing of hooks, which direct queries bypass.
99
]]>
1010
</standard>
1111
<code_comparison>
1212
<code title="Valid: Using WP_Query to retrieve data.">
1313
<![CDATA[
14-
$my_query = new WP_Query(
15-
[
16-
'post_type' => 'foo',
17-
]
14+
$page_query = new <em>WP_Query</em>(
15+
array(
16+
'post_type' => 'page',
17+
)
1818
);
1919
]]>
2020
</code>
2121
<code title="Invalid: Using a direct query to retrieve data.">
2222
<![CDATA[
23-
$results = $wpdb->get_results(
23+
$results = <em>$wpdb->get_results</em>(
2424
$wpdb->prepare(
25-
"SELECT * FROM x WHERE y = %s",
26-
'foo',
27-
),
25+
"SELECT * FROM %i
26+
WHERE post_type = %s",
27+
$wpdb->posts,
28+
'page'
29+
)
2830
);
2931
]]>
3032
</code>
3133
</code_comparison>
32-
3334
<standard>
3435
<![CDATA[
35-
You should not make direct database queries without caching.
36+
When a direct database call is necessary, it should be paired with caching. For read queries, this avoids running expensive queries multiple times. For write queries, invalidating relevant caches prevents stale data.
3637
]]>
3738
</standard>
3839
<code_comparison>
39-
<code title="Valid: The results of a direct database query are stored in the object cache.">
40+
<code title="Valid: Direct query results are cached and retrieved from cache when available.">
4041
<![CDATA[
41-
function foo() {
42-
global $wpdb;
43-
44-
$cached = wp_cache_get( 'foo' );
42+
function get_draft_ids() {
43+
$key = 'my_plugin_draft_ids';
44+
$cached = <em>wp_cache_get</em>( $key );
45+
4546
if ( false !== $cached ) {
4647
return $cached;
4748
}
4849
49-
$results = $wpdb->query(
50-
'SELECT x FROM foo'
50+
$results = $wpdb->get_col(
51+
"SELECT ID FROM $wpdb->posts
52+
WHERE post_status = 'draft'"
5153
);
5254
53-
wp_cache_set( 'foo', $results );
55+
<em>wp_cache_set</em>( $key, $results );
56+
5457
return $results;
5558
}
5659
]]>
5760
</code>
58-
<code title="Invalid: Direct database query is made without leveraging the object cache.">
61+
<code title="Invalid: Direct query without caching.">
5962
<![CDATA[
60-
function foo() {
61-
global $wpdb;
62-
return $wpdb->query( 'SELECT x FROM foo' );
63+
function get_draft_ids() {
64+
$results = $wpdb->get_col(
65+
"SELECT ID FROM $wpdb->posts
66+
WHERE post_status = 'draft'"
67+
);
68+
69+
return $results;
6370
}
6471
]]>
6572
</code>
6673
</code_comparison>
6774
<code_comparison>
68-
<code title="Valid: The results of a direct database query are stored in the object cache.">
75+
<code title="Valid: Cache is invalidated after a write query.">
6976
<![CDATA[
70-
function foo() {
77+
function update_post_title() {
7178
global $wpdb;
7279
73-
$cached = wp_cache_get( 'foo' );
74-
if ( false !== $cached ) {
75-
return $cached;
76-
}
77-
78-
$results = $wpdb->query(
79-
'SELECT x FROM foo'
80+
$wpdb->update(
81+
$wpdb->posts,
82+
array( 'post_title' => $title ),
83+
array( 'ID' => $post_id )
8084
);
8185
82-
wp_cache_set( 'foo', $results );
83-
return $results;
86+
<em>clean_post_cache</em>( $post_id );
8487
}
8588
]]>
8689
</code>
87-
<code title="Invalid: Incorrect implementation of caching with a direct database query.">
90+
<code title="Invalid: Write query without cache invalidation.">
8891
<![CDATA[
89-
function foo() {
92+
function update_post_title() {
9093
global $wpdb;
91-
$results = $wpdb->query(
92-
'SELECT * from foo'
93-
);
9494
95-
wp_cache_set( 'foo', $results );
96-
return $results;
95+
$wpdb->update(
96+
$wpdb->posts,
97+
array( 'post_title' => $title ),
98+
array( 'ID' => $post_id )
99+
);
97100
}
98101
]]>
99102
</code>
100103
</code_comparison>
101-
102104
<standard>
103105
<![CDATA[
104-
You should not make direct database queries to alter the database schema.
106+
Altering the database schema is discouraged, as it can break WordPress core, plugins, or themes that depend on the existing schema.
105107
]]>
106108
</standard>
107-
<code_comparison>
108-
<code title="Valid: Altering database schema using the dbDelta function.">
109-
<![CDATA[
110-
dbDelta( 'CREATE TABLE foo' );
111-
]]>
112-
</code>
113-
<code title="Invalid: Altering database schema using a direct database query.">
114-
<![CDATA[
115-
$wpdb->query( 'CREATE TABLE foo' );
116-
]]>
117-
</code>
118-
</code_comparison>
119109
</documentation>

0 commit comments

Comments
 (0)