|
5 | 5 | > |
6 | 6 | <standard> |
7 | 7 | <![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. |
9 | 9 | ]]> |
10 | 10 | </standard> |
11 | 11 | <code_comparison> |
12 | 12 | <code title="Valid: Using WP_Query to retrieve data."> |
13 | 13 | <![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 | + ) |
18 | 18 | ); |
19 | 19 | ]]> |
20 | 20 | </code> |
21 | 21 | <code title="Invalid: Using a direct query to retrieve data."> |
22 | 22 | <![CDATA[ |
23 | | -$results = $wpdb->get_results( |
| 23 | +$results = <em>$wpdb->get_results</em>( |
24 | 24 | $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 | + ) |
28 | 30 | ); |
29 | 31 | ]]> |
30 | 32 | </code> |
31 | 33 | </code_comparison> |
32 | | - |
33 | 34 | <standard> |
34 | 35 | <![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. |
36 | 37 | ]]> |
37 | 38 | </standard> |
38 | 39 | <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."> |
40 | 41 | <![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 | +
|
45 | 46 | if ( false !== $cached ) { |
46 | 47 | return $cached; |
47 | 48 | } |
48 | 49 |
|
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'" |
51 | 53 | ); |
52 | 54 |
|
53 | | - wp_cache_set( 'foo', $results ); |
| 55 | + <em>wp_cache_set</em>( $key, $results ); |
| 56 | +
|
54 | 57 | return $results; |
55 | 58 | } |
56 | 59 | ]]> |
57 | 60 | </code> |
58 | | - <code title="Invalid: Direct database query is made without leveraging the object cache."> |
| 61 | + <code title="Invalid: Direct query without caching."> |
59 | 62 | <![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; |
63 | 70 | } |
64 | 71 | ]]> |
65 | 72 | </code> |
66 | 73 | </code_comparison> |
67 | 74 | <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."> |
69 | 76 | <![CDATA[ |
70 | | -function foo() { |
| 77 | +function update_post_title() { |
71 | 78 | global $wpdb; |
72 | 79 |
|
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 ) |
80 | 84 | ); |
81 | 85 |
|
82 | | - wp_cache_set( 'foo', $results ); |
83 | | - return $results; |
| 86 | + <em>clean_post_cache</em>( $post_id ); |
84 | 87 | } |
85 | 88 | ]]> |
86 | 89 | </code> |
87 | | - <code title="Invalid: Incorrect implementation of caching with a direct database query."> |
| 90 | + <code title="Invalid: Write query without cache invalidation."> |
88 | 91 | <![CDATA[ |
89 | | -function foo() { |
| 92 | +function update_post_title() { |
90 | 93 | global $wpdb; |
91 | | - $results = $wpdb->query( |
92 | | - 'SELECT * from foo' |
93 | | - ); |
94 | 94 |
|
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 | + ); |
97 | 100 | } |
98 | 101 | ]]> |
99 | 102 | </code> |
100 | 103 | </code_comparison> |
101 | | - |
102 | 104 | <standard> |
103 | 105 | <![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. |
105 | 107 | ]]> |
106 | 108 | </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> |
119 | 109 | </documentation> |
0 commit comments