like query with prepare wordpress
You can use the following example to perform a "like" query with prepare in WordPress:
global $wpdb;
$search_term = 'your_search_term';
$prepared_statement = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE %s", '%' . $wpdb->esc_like($search_term) . '%');
$results = $wpdb->get_results($prepared_statement);
Explanation:
1. Retrieve the global WordPress database access class.
2. Set the search term to your desired value.
3. Use the $wpdb->prepare
method to create a prepared SQL statement. The %s
is a placeholder for a string input.
4. Utilize the $wpdb->esc_like
method to escape the search term and avoid SQL injection.
5. Execute the prepared statement using $wpdb->get_results
.