WP_Query
is not a general-purpose method for querying the database. It’s specifically designed for querying posts with the intention to loop over them and output templates for them.
If you just want arbitrary data from the wp_posts
table, then you can just write SQL and use the $wpdb
object to run it:
$query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type="attachment" ORDER BY ID LIMIT 100 OFFSET %d", $offset );
$attachments = $wpdb->get_results( $query );
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment ) {
$id = $attachment->ID;
$name = $attachment->post_title;
}
}
This uses the $wpdb->prepare()
method to safely add the $offset
variable to the SQL query, and then uses $wpdb->get_results()
method to run that query and return an array of objects for each result. The $wpdb->posts
part of the query allows you to target the wp_posts
table regardless of what the database prefix might be (Users can configure it to be something other than wp_
, and on multisite each site has a different posts table).
Keep in mind that the purpose of the wp_posts
table, and the attachment post type, is for that data to behave like posts, where things like the status and date are important. If you’re trying to use posts for something that isn’t like a ‘post’ then you probably shouldn’t be using the posts table.
You seem to want to list the name of everything in the Media Library, and I’m honestly not sure why you’d want to do that, it’s quite an odd thing to want to do. So if this isn’t your actual use-case I’d recommend updating your question with whatever it actually is.