Filter products from a specific custom meta data in Woocommerce shop page
You can use a custom function hooked in woocommerce_product_query_meta_query
filter hook, where you will replace _the_meta_key
in the code below, by your targeted meta_key
:
add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );function show_only_products_with_specific_metakey( $meta_query, $query ) { // Only on shop pages if( ! is_shop() ) return $meta_query; $meta_query[] = array( 'key' => '_the_meta_key', 'compare' => 'EXISTS' ); return $meta_query;}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Addition (related to your last comment):
To make it work for multiple meta values you need to use 'compare' => 'IN'
, like:
add_filter( 'woocommerce_product_query_meta_query', 'show_only_products_with_specific_metakey', 10, 2 );function show_only_products_with_specific_metakey( $meta_query, $query ) { // Only on shop pages if( ! is_shop() ) return $meta_query; $meta_query[] = array( 'key' => '_the_meta_key', 'value' => array('L','XL'), 'compare' => 'IN' ); return $meta_query;}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
you can use do_action( 'woocommerce_shop_loop' );
function ctm_loop_53631963(){ global $post; $meta = get_post_meta($post->ID, 'meta_value_key') if($meta !== 'desired_value'){ continue; }}do_action('woocommerce_shop_loop', 'ctm_loop_53631963');
read more about wordpress actions Plugin API/Action Reference