$wpdb->prepare()
use the same syntax for formatting like php’s printf()
. SSo you will need something like ... WHERE post_id IN (%d,%d,%d) ...
This will fit for three ids. But what if we got more or less than three ids? We will use the php tools to create the needed formatting string (assuming $ids
is an array with the ids):
Count the IDs
count( $ids )
Create a string with a ‘%d,’ for every ID in $ids
str_repeat( '%d,', count( $ids ) )
Now we have a string like %d,%d,%d,
. There is one comma to much at the end. Let’s remove it
$ids_format_string = rtrim( str_repeat( '%d,', count( $ids ) ), ',' );
And use this in this way '... WHERE post_id IN (' . $ids_format_string . ') ...'
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 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
- WPDB Placeholders and second argument for prepared statements
- 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
- Avoiding “Usage of a direct database call is discouraged”
- 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->prepare not working with update table prefix
- How to use prepare to query with variables
- How to execute mulitple statement SQL queries using $wpdb->query?
- $wpdb not returning data
- wpdb->get_row() / get_results – is it possible to return an object instances of my own class?
- How to email user after inserting the username in database in WordPress
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to fetch an array in $wpdb?
- What’s wrong with my $wpdb prepare?
- $wpdb select date range of posts
- Rolling Back from current WordPress version to previous one
- SQL query for custom taxonomy slugs
- Display fields as values in array from external SQL DB
- What is wrong with this wpdb update?
- using $wpdb to insert a form into a post
- wp_create_user not properly entering password
- Problem with creating tables for Plugin
- Foreach loop not working as expected – custom tables and references while submitting a form
- Query insert query executing multiple times
- Undefined offset: 0
- Connect to separate database using $wpdb and authenticate user
- How to access PostgreSQL using WPDB?
- Counting posts with argument without retrieving the posts
- how to assign content to a different author while loading content
- How to pass orderby params to $wpdb->prepare()?
- Register custom table for WP to use in a plugin
- 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
- wpdb->insert with special chars failing with collation utf8mb4_unicode_520_ci
- SQL queries to another wordpress site
- 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
- WPDB delivers wrong results from complex queries
- 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-postratings: list current user’s unrated posts
- Error inserting row into table
- How to update user profile or inputing any data to the wp db from external site?
- get only 1 wpdb and get taxonomy, post to next page [closed]