You can use the array directly because the $wpdb->prepare()
method can be called like vsprintf()
, which works like sprintf()
but accepts arrays of arguments.
$post_types = get_post_types();
unset( $post_types['attachment'], $post_types['nav_menu_item'] );
global $wpdb;
$replace_query = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )", $post_types ), ARRAY_A );
The problem with your code is that you get a string like this one:
"'page', 'attachment', 'revision', 'custom_css', 'customize_changeset'"
So, the IN
statement you get is like:
IN ( "'page', 'attachment', 'revision', 'custom_css', 'customize_changeset'" )
Instead of the correct one that would be an array of strings:
IN ( 'page', 'attachment', 'revision', 'custom_css', 'customize_changeset' )
Related Posts:
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- What’s wrong with my $wpdb prepare?
- wpdb query problem to access previous 3 days posts
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Custom Query – Based on user input
- prepare function sql safe method
- How do I query for posts by custom meta and those that have been stickied?
- Fastest and most efficient SQL query to check if UID exists
- $wpdb query for price in custom field value
- Run an update query in a function
- Wpdb generates too many queries
- How to display SQL query that ran in query?
- How to make an activities stream mixing posts and comments?
- Custom $wpdb Query for Custom Post Type by Category
- Can’t pass table to $wpdb->prepare
- WordPress creating excessive joins on meta_query with search
- $wpdb->get_var not returning a result
- cron job to auto delete posts of a specific post type older than x days
- Are database queries created using WordPress filters protected from SQL injection?
- Most effective use of DB querys
- Get the timout value of a saved transient?
- $wpdb->query() vs. $wpdb->get_results() vs. phpMyAdmin
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- wpdb::prepare() isn’t working
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- $wpdb doesn’t appear to work on page inside of a plugin
- Strange string in console from wpdb query
- Using $wpdb->update but confused on the WHERE in and SET
- How to correctly pass values to wpdb->prepare()?
- Make one query for adding entries to database
- Get stock by custom meta field on all Woocommerce variable products
- How to execute a SQL-query which contains multiple queries using $wpdb->query?
- Alter a specific query on WordPress
- How do I see the mysql query generated by get_posts( $args )?
- sql select query in wordpress ‘page’ [closed]
- wpdb->term_taxonomy count posts from both published and private posts?
- wpdb print all post meta
- Plugin will sort users by usermeta
- problem with sql query in wordpress plugin
- Attempt to improve WP search, can someone check my SQL query?
- How to write: $wpdb->update having WHERE NOT value pair in the array
- Use mysql_query instead of $wpdb query
- wpdb COALESCE won’t work
- Custom SQL Query passing array to WHERE p.id
- Get posts from category with custom query
- Custom Query returns array, but I can’t see it’s elements
- how to insert missing tags into the posts through mySQL?
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- WordPress SQL search, how to handle SQL Injection?
- exclude pingbacks from wordpress SQL query
- Updating with $qpdb->query() always returns 0 rows affected
- Why my query does not run with prepare
- WordPress SQL query to tag all posts containing a specific word on title
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- Select column name dynamically mySQL Query
- Help with Related Posts Function
- wpdb update multiple row?
- Formatting a date/time returned from a custom $wpdb query
- Improve performance of slow query
- Change pure SQL database query to WordPress post query?
- How do I get specific readable results from this query and array results
- Cannot get sql request from Query object?
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Customising the default wordpress search functionality
- Display data from phpMyAdmin with WordPress
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Delete oldest wordpress post (SQL query)
- SQL to Query the db and return all posts and it’s metas
- SQL query to delete users with multiple meta keys and comments
- Nested select statements not working
- Speed up search query that searches in post meta?
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Exclude Posts from a Widget
- How to make MySQL search queries with quotes
- Use $wpdb->get_results with filter based on array
- How i make a custom sql query for Woocommerce
- Display result from custom post meta query
- Wpdb empty result only if language is Russian
- How to fix this insert? Don’t work for me!
- How to display a specific category using a custom Query in WordPress?
- Update vs Insert logic but the last key is always inserted?
- Slow query when joining wp_posts with a lookup-table?
- There is way to improve this query? wordpress get post data and some meta data
- $wpdb: how to use a query as a base for another query?
- Custom Query for searching through custom fields
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Mysql query and odd results
- learn to run wpdb class
- How to reference same column name but different table in custom db query
- Retrieving custom field array value through db query
- How to get one result using wpdb class?
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- Delete query won’t run
- Where can I find the SQL to get the most used information by wordpress database?
- Custom Query for wp_posts using wp_postmeta
- 3 queries to update WordPress
- update $wpdb one query