1

I used a big part of this code to add a meta_query in the WooCommerce product query. I don't get any errors, but the products are NOT getting filtered by the meta. It still shows all products.

function yl_custom_product_query( $query ) {
    // check if the user is requesting an admin page 
    // or current query is not the main query
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    global $product;

    $blog_id = get_current_blog_id();

    if ($blog_id == '1') {
        $subsite = '_visibility_3rdmillennium';
    } elseif ($blog_id == '2') {
        $subsite = '_visibility_fight2win';
    } elseif ($blog_id == '3') {
        $subsite = '_visibility_muaythai';
    } elseif ($blog_id == '4') {
        $subsite = '_visibility_taekwondo';
    } elseif ($blog_id == '5') {
        $subsite = '_visibility_xprtfightgear';
    } elseif ($blog_id == '6') {
        $subsite = '_visibility_hayabusashop';
    } elseif ($blog_id == '7') {
        $subsite = '_visibility_kmushop';
    }

    $meta_query = array();

    // add meta_query elements
    if ( !empty( get_query_var( $subsite ) ) ) {
        $meta_query[] = array( 'key' => $subsite, 'value' => get_query_var( 'yes' ), 'compare' => 'LIKE' );
    }

    // unused meta_query for now
    if ( !empty( get_query_var( 'key2' ) ) ) {
        $meta_query[] = array( 'key' => 'key2', 'value' => get_query_var( 'key2' ), 'compare' => 'LIKE' );
    }

    if ( count( $meta_query ) > 1 ) {
        $meta_query['relation'] = 'AND';
    }

    if ( count( $meta_query ) > 0 ) {
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'yl_custom_product_query' );

But, when I change:

    // add meta_query elements
    if ( !empty( get_query_var( $subsite ) ) ) {
        $meta_query[] = array( 'key' => $subsite, 'value' => get_query_var( 'yes' ), 'compare' => 'LIKE' );
    }

to

    // add meta_query elements
    if ( !empty( $subsite ) ) {
        $meta_query[] = array( 'key' => $subsite, 'value' => 'yes', 'compare' => 'LIKE' );
    }

(so removing get_query_var twice) the meta_query works great but all static pages suddenly become unreachable...

What am I missing here?

Yorlinq
  • 131
  • 14

0 Answers0