I have manually edited a WordPress WooCommerce hook which hides all out of stock products. The problem is when all products in category are marked as out of stock, I get HTTP 500 error. How can I avoid this?

My code:

function hide_products($query) {
    $meta_query = $query->get( 'meta_query' );
    $meta_query[] = array(
            'key'       => '_stock_status',
            'compare'   => 'NOT IN',
            'value'     => 'outofstock'
    $query->set( 'meta_query', $meta_query );


  • I have an idea how to go... Just count the results and if results = 0, redirect to another page before calling $query->set(). Not perfect, but how to count the results? – Ru Leva Sep 25 '19 at 20:27

Your Answer

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

Browse other questions tagged or ask your own question.