Change the images query to compatibility with WPML plugin
-
the plugin “WPML media” clone an image for each language. So if you run a query without filters the same image is regenerated more times.
Also I find wrong execute query without filters if there is not a real reason.The Change Proposal has been tested on a database of 2000 images and work.
I also added a filter on the type so that only processes the images.
But it would make sense to add options in a text area with the types allowed.In function force_regenerate_interface replace directly query with
standard API and set ‘fields’ = ‘ids’ so will return only ids./** OLD **/ // Directly querying the database is normally frowned upon, but all // of the API functions will return the full post objects which will // suck up lots of memory. This is best, just not as future proof. <-- 'fields' => 'ids' return only id. /*if (!$images = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC")) { echo ' <p>' . sprintf(__("Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'force-regenerate-thumbnails'), admin_url('upload.php?post_mime_type=image')) . "</p></div>"; return; } $images = array(); foreach ( $query_images->posts as $image ) { $images[] = wp_get_attachment_url( $image->ID ); }*/ // Generate the list of IDs /*$ids = array(); foreach ($images as $image) { $ids[] = $image->ID; } $ids = implode(',', $ids);*/ /** NEW **/ $supported_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon'); $query_images_args = array( 'post_type' => 'attachment', 'post_mime_type' => $supported_mimes, 'post_status' => 'any', 'nopaging' => true, 'fields' => 'ids', 'orderby' => 'date', 'order' => 'DESC' ); $query_images = new WP_Query( $query_images_args ); if (empty($query_images->posts)) { echo ' <p>' . sprintf(__("Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'force-regenerate-thumbnails'), admin_url('upload.php?post_mime_type=image')) . "</p></div>"; return; } $ids = implode(',', $query_images->posts);
The topic ‘Change the images query to compatibility with WPML plugin’ is closed to new replies.