Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active January 15, 2025 21:08
Show Gist options
  • Select an option

  • Save wpscholar/5026f5053c898a88c01d to your computer and use it in GitHub Desktop.

Select an option

Save wpscholar/5026f5053c898a88c01d to your computer and use it in GitHub Desktop.
$wpdb->prepare() example
<?php
global $wpdb;
$query = $wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type = %s",
$post->post_type
);
$results = $wpdb->get_results( $query );
@lbonomo

lbonomo commented Jan 15, 2025

Copy link
Copy Markdown

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/

@wpscholar

Copy link
Copy Markdown
Author

@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