You can use
$args = array( 'post_type' => 'post' ,'posts_per_page' => 10, 'category_name' => 'orange' );
$query = new WP_Query( $args );
printf( '<h2>Generated SQL:</h2><pre>%s</pre>', $query->request );
to display the generated SQL
query. The above example will give you:
<h2>Generated SQL:</h2>
<pre>
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (123) )
AND wp_posts.post_type="post"
AND (wp_posts.post_status="publish" OR wp_posts.post_status="private")
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10
</pre>
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
- 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)
- 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)
- Matching database content types to PHP types
- Use wpdb->prepare for `order by` column name
- wpdb->prepare function remove single quote for %s in SQL statment
- Saving custom form data into database
- wp-content/db.php : where is this file?
- $wpdb->get_results() returns good result only in first foreach iteration
- $wpdb->insert – inserting multiple rows
- WPDB->insert with special characters
- $wpdb->prepare not working with update table prefix
- How to use prepare to query with variables
- get posts and postmetas in assoc array
- wpdb->get_row() / get_results – is it possible to return an object instances of my own class?
- $wpdb – do you have to use foreach even if there will always only be one result?
- wpdb_prepare with multiple or condition
- $wpdb->insert query changes to SHOW COLUMNS query Output
- Fixing wpdb->get_results and wpdb->prepare?
- How does the WXR file differ from the WPDB?
- What’s wrong with my $wpdb prepare?
- Display fields as values in array from external SQL DB
- What is wrong with this wpdb update?
- Constructing a dynamic WPDB query with multiple LIKEs
- Counting posts with argument without retrieving the posts
- Use $wpdb or other PHP script method to find/replace in WP database
- WordPress “wpdb->update” – Append Text Value
- Need help converting get_user_meta [keys] into own array
- Can’t call WPDB inside RSS template
- wpdb prepare sql problem
- Query and get meta as object(stdClass) on wp postmeta table?
- check that the data exists before sending it to wpdb
- no result returns when using $wpdb->get_results with where clause
- Customising the default wordpress search functionality
- SQL queries to another wordpress site
- PHP | left join avg query give me Array()
- update not working using wpdb
- get_posts() SQL Injection
- og:image functions.php
- $wpdb select returns empty array
- Show error messages to a user when database insert fails
- Intermittent problem writing update_user_meta
- How to get specific table by current user login
- Combine posts and postmeta
- WPDB delivers wrong results from complex queries
- I can’t write in my DB using $wpdb->insert
- wp_commentdata: error in documentation?
- Echo only gives me the first part of string
- esc_sql inserting weird chars to DB
- Which query method to use? (edit- wpdb syntax problems)
- Retriving array size from serialized data
- Can’t get expected result from a wpdb query
- selecting row using wpdb which contain special symbols
- Query Problem in Clustom Plugin
- Table wont load into WPDB
- WPDB prepare – like % – placeholders?