@@ -27,7 +27,7 @@ public function __construct() {
27
27
$ pos_only_products = woocommerce_pos_get_settings ( 'general ' , 'pos_only_products ' );
28
28
29
29
if ( $ pos_only_products ) {
30
- add_filter ( 'posts_where ' , array ( $ this , 'hide_pos_only_products ' ), 10 , 2 );
30
+ add_action ( 'pre_get_posts ' , array ( $ this , 'hide_pos_only_products ' ) );
31
31
add_filter ( 'woocommerce_variation_is_visible ' , array ( $ this , 'hide_pos_only_variations ' ), 10 , 4 );
32
32
add_action ( 'woocommerce_store_api_validate_add_to_cart ' , array ( $ this , 'store_api_prevent_pos_only_add_to_cart ' ) );
33
33
@@ -76,22 +76,26 @@ public function product_set_stock( WC_Product $product ): void {
76
76
*
77
77
* @return string
78
78
*/
79
- public function hide_pos_only_products ( $ where , $ query ) {
80
- // Ensure this only runs for the main WooCommerce shop queries
81
- if ( ! is_admin () && $ query ->is_main_query () && ( is_shop () || is_product_category () || is_product_tag () ) ) {
82
- global $ wpdb ;
79
+ public function hide_pos_only_products ( $ query ) {
80
+ // Ensure this only runs for the main WooCommerce queries on product-related pages
81
+ if ( ! is_admin () && $ query ->is_main_query () && ( is_shop () || is_product () || is_post_type_archive ( 'product ' ) || is_product_taxonomy () ) ) {
83
82
84
83
$ settings_instance = Settings::instance ();
85
84
$ settings = $ settings_instance ->get_pos_only_product_visibility_settings ();
86
85
87
86
if ( isset ( $ settings ['ids ' ] ) && ! empty ( $ settings ['ids ' ] ) ) {
88
- $ exclude_ids = array_map ( 'intval ' , (array ) $ settings ['ids ' ] );
89
- $ ids_format = implode ( ', ' , array_fill ( 0 , count ( $ exclude_ids ), '%d ' ) );
90
- $ where .= $ wpdb ->prepare ( " AND {$ wpdb ->posts }.ID NOT IN ( $ ids_format) " , $ exclude_ids );
87
+ $ exclude_ids = array_map ( 'intval ' , (array ) $ settings ['ids ' ] ); // Sanitize IDs as integers
88
+
89
+ // Merge any existing excluded IDs with the new ones
90
+ $ existing_excludes = $ query ->get ( 'post__not_in ' );
91
+ if ( ! is_array ( $ existing_excludes ) ) {
92
+ $ existing_excludes = array ();
93
+ }
94
+
95
+ // Set the post__not_in query parameter to exclude specified IDs
96
+ $ query ->set ( 'post__not_in ' , array_merge ( $ existing_excludes , $ exclude_ids ) );
91
97
}
92
98
}
93
-
94
- return $ where ;
95
99
}
96
100
97
101
/**
0 commit comments