Last active
January 15, 2025 21:08
-
-
Save wpscholar/5026f5053c898a88c01d to your computer and use it in GitHub Desktop.
$wpdb->prepare() example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
$query = $wpdb->prepare( | |
"SELECT * FROM {$wpdb->posts} WHERE post_type = %s", | |
$post->post_type | |
); | |
$results = $wpdb->get_results( $query ); |
@lbonomo - I believe my code snippet here predates that feature, but yes, that appears as though it may be the latest best practice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Why you don't use ?
$query = $wpdb->prepare( "SELECT * FROM %i WHERE post_type = %s", $wpdb->posts, $post->post_type );
https://developer.wordpress.org/reference/classes/wpdb/prepare/