WooCommerce admin filter products by image
A client recently asked me to add a filter to the WooCommerce admin products list so they can filter items based on if the item had an image or not. This allowed them to easily find products which had missing images so they could correct.
Why not take a look how to filter by on sale items?
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
/* * Woocommerce Filter by has image */ function custom__woocommerce_filter_by_hasimage($output) { global $wp_query; $selected = 0; if (isset($_GET['product_hasimage'])) { $selected = (int)$_GET['product_hasimage']; } $output .= ' <select name="product_hasimage" id="dropdown_product_has_image"> <option value="">Filter by image</option> <option value="1" ' . (($selected === 1) ? 'selected="selected"' : '') . '>Has Image</option> <option value="2" ' . (($selected === 2) ? 'selected="selected"' : '') . '>Has No Image</option> </select> '; return $output; } add_action('woocommerce_product_filters', 'custom__woocommerce_filter_by_hasimage'); /* * Woocommerce Filter by on has image where statement */ function custom__woocommerce_filter_by_has_image_where_statement($where, $query) { global $wpdb; // Only trigger if required if (!is_admin() || $_GET['post_type'] != "product" || !isset($_GET['product_hasimage']) || (int)$_GET['product_hasimage'] <= 0) { return $where; } // Is filter applied if ($_GET['product_hasimage'] > 0) { // What condition to check $condition = 'IN'; if ($_GET['product_hasimage'] == 1) { $condition = 'NOT IN'; } // Query $queryStr = ' SELECT ID FROM ( SELECT ID FROM djqns_posts WHERE ID IN ( SELECT tp.ID FROM djqns_posts tp LEFT OUTER JOIN djqns_postmeta tpm ON tpm.post_id = tp.ID AND tpm.meta_key = "_thumbnail_id" WHERE 1=1 AND ( tpm.meta_value = "0" OR tpm.meta_key IS NULL ) AND tp.post_type = "product" ORDER BY tp.ID ) AND post_type = "product" ORDER BY ID ) tmp '; $where .= ' AND (' . $wpdb->posts . '.ID ' . $condition . ' (' . $queryStr . ')) '; } return $where; } add_filter('posts_where' , 'custom__woocommerce_filter_by_has_image_where_statement', 10, 2); |
This code is provided as it and may require changes to work for your needs.
zendarol
sotty your code in not for this article
can you post real code WooCommerce admin filter products by image
Shane Rutter
Unsure what happened there, I have reverted the post. Thanks.