admin管理员组

文章数量:1278984

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' );

本文标签: woocommerce offtopicunable to customize query to get filtered products by metakey