The get_posts()
function creates an instance of WP_Query inside the function and simply returns the array of results. This WP_Query instance doesn’t override the global $wp_query
and is out of scope by the time you need it. In short, I don’t see a way how you can get the requested SQL via get_posts()
.
An alternative is to simply create your own instance of WP_Query and pass your get_posts()
arguments to it:
$query = new WP_Query( array(
'post_type' => 'inst',
'post_status' => 'publish'
'posts_per_page' => -1,
'order' => 'ASC', // sort_order
'orderby' => 'post_title', // sort_column
) );
That will give you the whole WP_Query object where you can output the SQL:
$query_sql = $query->request;
Finally, if you’re already creating a WP_Query you don’t need the get_posts()
call at all at that point.
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
the_content();
}
// Reset the global $wp_query to what it was originally.
wp_reset_postdata();
}