不WordPress将not 在这种情况下,防止SQL注入。您需要自己这样做,使用$wpdb->esc_like
和$wpdb->prepare
:
if ( isset( $_GET[\'q\'] ) ) {
// WordPress forces magic quotes (god knows why), unslash it
$value = wp_unslash( ( string ) $_GET[\'q\'] );
// Escape like wildcards so that MySQL interprets them as literals
$value = $wpdb->esc_like( $value );
// Create our "true" like query
$value = "%$value%";
// Now inject it safely
$where .= $wpdb->prepare(
" AND ( ( $wpdb->posts.post_title LIKE %s ) OR ( $wpdb->posts.post_content LIKE %s ) )",
$value,
$value
);
}