Home > front end >  WooCommerce hide product without thumbnail
WooCommerce hide product without thumbnail

Time:04-13

Is there any way to hide products that have no thumbnail, I've tried this code but doesn't work.

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $query ) {

    $query->set( 'meta_query', array( array(
       'key' => '_thumbnail_id',
       'value' => '0',
       'compare' => '>'
    )));

}

CodePudding user response:

function woocommerce_product_query_has_thumbnail( $query ) {
    $query->set( 'meta_key', '_thumbnail_id' );
}
add_action( 'woocommerce_product_query', 'woocommerce_product_query_has_thumbnail' );

Add this code into your active theme functions.php file

This will make sure that the posts with _thumbnail_id has a value.

CodePudding user response:

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

This will ensure that the meta_value has a value.

If you want something more general.

function pre_get_posts( $query ) {
    if ( !is_admin() && $query->is_main_query() && ( $query->get('post_type') == 'product' ) ) {
       $query->set( 'meta_key', '_thumbnail_id' );
    }
}
add_action( 'pre_get_posts', 'pre_get_posts' );
  • Related