Title: Bulk Optimize via WP-CLI?
Last modified: April 13, 2018

---

# Bulk Optimize via WP-CLI?

 *  Resolved [hoyer801](https://wordpress.org/support/users/hoyer801/)
 * (@hoyer801)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/bulk-optimize-via-wp-cli/)
 * I’ve been trying to write a script so I can invoke the bulk optimize feature 
   on a large multisite.
 * I’ve written this, and it looks like this is the right routine, but when I’m 
   testing it on individual files, I’m not seeing any reduction in file size.
 * Am I on the right track, or is there a better way to do this?
 *     ```
       $sites = get_sites();
   
       foreach($sites as $site){
           switch_to_blog($site->blog_id);
   
           $args = ['post_type' => 'attachment', 'numberposts' => -1];
           $attachments = get_posts($args);
   
           foreach($attachments as $attachment){
               $id = $attachment->ID;
               $metadata = wp_get_attachment_metadata($id);
               $optimus_metadata = Optimus_Request::optimize_upload_images($metadata, $id);
               if ( !empty($optimus_metadata['optimus']['error']) ) {
                   var_dump(array('error' => $optimus_metadata['optimus']['error']));
                   exit;
               }
   
               /* check if optimus array empty */
               if ( empty($optimus_metadata['optimus']) ) {
                   var_dump(array('error' => __("Internal error", "optimus")));
                   exit;
               }
               /* update metadata */
               update_post_meta($id, '_wp_attachment_metadata', $optimus_metadata);
   
           }
   
           restore_current_blog();
       }
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [hoyer801](https://wordpress.org/support/users/hoyer801/)
 * (@hoyer801)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/bulk-optimize-via-wp-cli/#post-10179833)
 * I have purchased the HQ Pro license and its enabled.
 *  [codyarsenault](https://wordpress.org/support/users/codyarsenault/)
 * (@codyarsenault)
 * [8 years, 1 month ago](https://wordpress.org/support/topic/bulk-optimize-via-wp-cli/#post-10190798)
 * Have you also referenced the PHP library for the Optimus API? [https://github.com/keycdn/php-optimus-api](https://github.com/keycdn/php-optimus-api)
 *  Plugin Author [KeyCDN](https://wordpress.org/support/users/keycdn/)
 * (@keycdn)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/bulk-optimize-via-wp-cli/#post-10635411)
 * This is possible in v1.6.0, which will be released soon.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Bulk Optimize via WP-CLI?’ is closed to new replies.

 * ![](https://ps.w.org/optimus/assets/icon-256x256.png?rev=976959)
 * [Optimus - WordPress Image Optimizer](https://wordpress.org/plugins/optimus/)
 * [Support Threads](https://wordpress.org/support/plugin/optimus/)
 * [Active Topics](https://wordpress.org/support/plugin/optimus/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/optimus/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/optimus/reviews/)

 * 3 replies
 * 3 participants
 * Last reply from: [KeyCDN](https://wordpress.org/support/users/keycdn/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/bulk-optimize-via-wp-cli/#post-10635411)
 * Status: resolved