I am trying to get products with a custom field with value condition from DB.
Custom field is named: "_filtered_product".
I want to get products only with value = 1
This is SQL query I have written.
$view_low_filt_sql =
"LEFT JOIN $wpdb->postmeta manage ON (p.ID = manage.post_id AND manage.meta_key = '_manage_stock')
LEFT JOIN $wpdb->postmeta stock ON (p.ID = stock.post_id AND stock.meta_key = '_stock')
LEFT JOIN $wpdb->postmeta threshold ON (p.ID = threshold.post_id AND threshold.meta_key = '_low_inventory_number')
LEFT JOIN $wpdb->postmeta filtered ON (p.ID = filtered.post_id AND filtered.meta_key = '_filtered_product')
WHERE (p.post_type = 'product' OR p.post_type = 'product_variation')
AND (p.post_status = 'publish' OR p.post_status = 'private' OR p.post_status = 'draft')
AND manage.meta_value = 'yes'
AND threshold.meta_value IS NOT NULL
AND filtered.meta_value = '1'
AND IFNULL(CAST(stock.meta_value AS SIGNED),0) <= CAST(threshold.meta_value AS SIGNED)
";