Sort products without thumbnail in WooCommerce shop page

I would like to create a function that instead of hiding the products without thumbnails, would be ordered in such a way that the products with the highlighted image appear first on the shop page while those without the highlighted image appear at the end. I tried to modify an already existing function:

    function woocommerce_product_query( $q ) {
        $q->set( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value_num');
        $q->set('order', 'DESC');
    }
    add_action( 'woocommerce_product_query', 'woocommerce_product_query' );

I tried adding orderby to this already existing function too but nothing happens, keep hiding the products from me.

Shop page
In this photo the function only hides the products without thumbnail.
I don’t know how to solve …

2 Answers
2

I would try to rename the custom function and add meta_query too! So it’d be like this:


function my_shop_custom_products_query( $q ) {
        $q->set( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value_num');
        $q->set('order', 'DESC');
        $q->set( 'meta_query', array( array(
           'key' => '_thumbnail_id',
           'compare' => '>=',
           'value' => '0'       
        )));
}

add_action( 'woocommerce_product_query', 'my_shop_custom_products_query' );

If for some reason it didn’t work, then try to replace meta_value_num with meta_value and see if that’d work!

Leave a Comment