Just use %s
for string and %d
for digit replacements. Do not forget to use the proper esc_*()
functions (use full text search for esc_
in the function reference).
global $wpdb;
$wpdb->show_errors = true;
$wpdb->suppress_errors = false;
! defined( 'DIEONDBERROR' ) and define( 'DIEONDBERROR', true );
$sql = <<<SQL
SELECT ID
FROM {$wpdb->posts}
WHERE post_type="attachment"
AND ID IN (%s)
ORDER BY menu_order
ASC
SQL;
# Make sure to `esc_*()` the arguments properly!
$statement = $wpdb->prepare( $sql, implode( "','", $slideshow_imgs ) );
# Example: Query … there are other methods as well:
$wpdb->query( $statement );
# DUMP the result and any possible errors
var_dump( $wpdb->last_query, $wpdb->last_error );
Related Posts:
- How to use $wpdb to delete in a custom table
- get_results on large datasets
- Is there a (better) way to access $wpdb results?
- Using $wpdb generates DB error
- How do you use prepare when asking for a list of id’s
- how to execute different sql query in non-sanitized $wpdb->get_results function
- WPDB update row with != in where clause
- How to update records using $wpdb?
- Need help writing a $wpdb query
- show badge with count for pending items in custom post type
- WP Sql query multiple where clause
- WordPress SQL query – returning ‘true’ ‘false’ or ‘null’
- Modify the structure of data returned by $wpdb
- Syntax for $wpdb->prepare when searching in two columns
- Confused by $wpdb->prepare
- How to display user_nicename and usermeta values by custom query in WordPress?
- Optimizing WordPress Queries – Removing Group By ID
- How can I combine one field using wpdb and group by?
- $wpdb->prepare with ON DUPLICATE KEY UPDATE
- how to use $wpdb->prepare to update a custom table
- Increment integer field in database when WHERE needs to be dynamic [closed]
- Custom SQL query ORDER BY term_order
- Custom $wpdb returns unexpected time based results
- How to left join meta in queries [closed]
- wpdb->get_row is selecting the variable as a column name
- CREATE TABLE with dbDelta does not create table
- $wpdb query outputs php code instead of executing it
- wpdb query not working
- WordPress wpdb->insert returns int(0) => doesn’t insert anything, no errors!
- WPDB SQL Ignore `post_status` Parameter
- how to list all post that are in the custom taxonomy using $wpdb
- WPDB SQL query SELECT from category
- How to use WHERE NOT EXISTS query to avoid duplicate entry using $wpdb to save in custom table?
- Creating an Angular factory from custom database table
- wpdb->update update the entire table instead of one row
- How do I update post based on meta_key in another table?
- How to set up prepared query using IN statement
- Custom database query to validate data
- Alter the main search query to search posts by coauthor user name
- Creates only one table and not the other
- Protect custom form from SQL injection
- looking for a way to allow users to backup the plugin db data(save as)
- SQL Query to select post title & post ID from a particular category
- Get comments after specific date
- query using wpdb in wordpress gets me no result
- Get count of rows based if column exists in two different tables
- query_vars doesn’t return query string (trying to get data from $wpdb)
- WPDB Insert or if exists Update
- How do I check for a duplicate record before inserting using wpdb
- WPDB prepare – like % – placeholders?
- Avoiding “Usage of a direct database call is discouraged”
- prepare() not working
- passing variables as parameters to stored procedures via wpdb from php-script
- $wpdb->query() multiple query support
- Set media metadata (i.e. “dimensions” field) on SVG file after extracting it with a filter
- $wpdb not returning data
- $wpdb Query Result not coming but in phpmyadmin it works [closed]
- Join inside a wpdb query.. confused!
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- Why doesn’t my insert query work?
- How to prevent $wpdb->prepare stripping a leading zero in variable value?
- Generate a unique hash/number for tracking on $wpdb insert
- $wpdb select date range of posts
- wpdb->insert and stripslashes against sql injection
- Rolling Back from current WordPress version to previous one
- $wpdb select query by month, post type, and taxonomy term
- WPDB – How to search a column in a table
- Foreach loop not working as expected – custom tables and references while submitting a form
- Undefined offset: 0
- Connect to separate database using $wpdb and authenticate user
- $wpdb not being defined in function: Fatal error: Call to a member function query() on a non-object
- How to obtain a reference to $table_prefix in $wpdb object
- Trying to Connect to different database
- How to pass orderby params to $wpdb->prepare()?
- Register custom table for WP to use in a plugin
- Use $wpdb or other PHP script method to find/replace in WP database
- How to get INSERT errors from $wpdb?
- Display category names on edit user profile using $wpdb
- wpdb and acf via wp rest api
- wpquery properties last_query and last_result : should these be public or private?
- wpdb prepare sql problem
- Query and get meta as object(stdClass) on wp postmeta table?
- wpdb->insert with special chars failing with collation utf8mb4_unicode_520_ci
- og:image functions.php
- Show error messages to a user when database insert fails
- Debugging db calls: $wpdb->insert works on test site, not on identical production site
- Combine posts and postmeta
- Include post_status check within $wpdb query
- wpdb get_row database query inquiry
- Depreciated Call -> Function wpdb::escape()
- Inserting and updating rows with wpdb indreases integer fields by 1 point sometimes
- Retrieve a list of users based on some conditions
- wp_commentdata: error in documentation?
- wp-postratings: list current user’s unrated posts
- Error inserting row into table
- Which query method to use? (edit- wpdb syntax problems)
- Retriving array size from serialized data
- get only 1 wpdb and get taxonomy, post to next page [closed]
- Table wont load into WPDB
- WPDB prepare – like % – placeholders?