Reputation: 285
I want to change products in woocommerce product page loop with filter
i'm try filter from url wordpress.test/shop?filterbyAge=23
, now i added code in functions file but not working
i created felid with ACF plugin in product
function updateQueryByAgeFilter( $q ){
if (isset($_GET['filterbyAge'])) {
$q->set('meta_query', [
[
'key' => 'book_age_group',
'value' => $_GET['filterbyAge'],
'compare' => '='
]
]);
}
}
add_action( 'woocommerce_product_query', 'updateQueryByAgeFilter' );
Please help me to solve this issue
Upvotes: 0
Views: 755
Reputation: 29650
Merging the query seems to be missing
So you get:
// Change the shop query
function action_woocommerce_product_query( $q, $query ) {
// Returns true when on the product archive page (shop) & isset
if ( is_shop() && isset( $_GET['filterbyAge'] ) ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query' );
// Settings
$key = 'book_age_group';
$value = $_GET['filterbyAge'];
$meta_query[] = array(
'key' => $key,
'value' => $value,
'compare' => '='
);
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'action_woocommerce_product_query', 10, 2 );
Upvotes: 2