When using the 'fields' => 'ids' parameter, is it better to create a new WP_Query in the code below rather than doing get_posts?
How would the loop look if I used WP_Query?
function myplugin_delete_image_items( $postid ) {
$args = array(
'post_type' => 'myplugin_image_item',
'cache_results' => false, // Disable caching, used for one-off queries
'no_found_rows' => true, // We don't need pagination, so disable it
'fields' => 'ids', // Returns post IDs only
'meta_query' => array(
array(
'key' => 'myplugin_image_id',
'value' => $postid,
'compare' => 'LIKE'
)
)
);
$image_item_ids = get_posts( $args );
foreach( $image_item_ids as $image_item_id ) {
$purge = wp_delete_post( $image_item_id, true ); // delete the image item, skipping trash
}
wp_reset_postdata();
}
get_posts
itself just creates a newWP_Query
, all parameters being equal it's exactly the same.