1

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 ...

0

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!

1
  • Hi thanks for the reply. Maybe I'll have expressed myself badly. I would like the woocommerce thumbnails, where no featured images have been set, to be seen anyway but in last position. – Naycom Feb 5 at 11:51
0

I have implemented the code but the sorting is done alphabetically. While I would like to sort by product entry date:

function my_shop_custom_products_query( $q ) {
        $q->get( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value');
        $q->set('order', 'DESC');
        $q->set( 'meta_query', array(  'relation' => 'OR', array(
            
           'orderby' => 'meta_value',
            'order' => 'DESC',
            'key' => '_thumbnail_id',
           'compare' => 'NOT EXISTS',
     //  'value' => '0'       
  
         
            
  
        ),
  array(
     'key' => '_thumbnail_id',
           'compare' => '>=',
       'value' => '0',       
  'orderby' => 'meta_value',
       'order' => 'DESC'
  )),
); 
}

add_action( 'woocommerce_product_query', 'my_shop_custom_products_query' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.