woocommerce offtopic – unable to customize query to get filtered products by meta_key

Question

i am new to wp and I tried to filter product inside archive-product.php woocommerce shop page by using product meta key and value but unable to do that. WordPress version i am using 5.8.1 and woocommerce version 5.5.2 . I also tried it by adding acf field in product post but still no response. here’s the code that i tried

$q->set('meta_query', [
     [
        'key' => 'meta_key',   // used target meta key 
        'value' => 'meta_value',    // dynamic variable
        'compare' => '='  // also used LIKE in compare
      ]
    ]); 

here is how i am using this code inside functions.php (child theme)file

function so_20990199_product_query( $q ){ 
    if (isset($_GET['filterbyAge'])) {
    
        $q->set('meta_query', [
            [
                'key' => 'book_age_group',
                'value' => $_GET['filterbyAge'],
                'compare' => '='
            ]
        ]);
    }
}
add_action( 'woocommerce_product_query', 'so_20990199_product_query' );

0
Amanpreet Singh 1 month 2021-10-25T23:22:30-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse