Filter to exclude specific products from WooCommerce recent products widget not
-
Hello,
I am currently trying to exclude specific products from the WooCommerce recent products widget. Specifically, I want to prevent products with the custom meta field
_age_limit
set to “大人向け(ie,Adult)” from being displayed.I added the following code to my
functions.php
, but it’s not working as expected:add_filter('woocommerce_shortcode_products_query', 'exclude_adult_products_from_recent_products', 10, 3);
function exclude_adult_products_from_recent_products($query_args, $atts, $loop_name){
if( $loop_name == 'recent_products' ){
$query_args['meta_query'][] = array(
'key' => '_age_limit',
'value' => '大人向け',
'compare' => '!=',
);
}
return $query_args;
}- WordPress version: 6.6.2
- WooCommerce version: 9.3.1
- The
_age_limit
field is a custom meta field for the products. - The theme I’m using is the official WooCommerce Storefront theme.
I have confirmed that the tags are correctly stored in the database, and when I tested the SQL query directly in phpMyAdmin, it returned the expected results. Here is the SQL query I used:
SELECT * FROM ierd_postmeta WHERE meta_key = ‘_age_limit’ AND meta_value = ‘大人向け’;
The query successfully returns the expected products with the tag “大人向け.” However, the WooCommerce filter code doesn’t seem to exclude these products from the recent products widget.
Below is how I added the custom meta field for age limit in
functions.php
::function custom_product_general_fields() {
woocommerce_wp_select( array(
'id' => '_age_limit',
'label' => __( 'Age Limit', 'woocommerce' ),
'options' => array(
'全年齢' => __( '全年齢', 'woocommerce' ),
'大人向け' => __( '大人向け', 'woocommerce' ),
),
));
}
add_action( 'woocommerce_product_options_general_product_data', 'custom_product_general_fields' );I am really struggling with this issue, and I’m not sure why this filter isn’t working as expected. Could someone please help me?
Any advice on how to debug or resolve this problem would be greatly appreciated.Thank you so much for your assistance!
The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.