@@ -27,7 +27,7 @@ public function __construct() {
2727 $ pos_only_products = woocommerce_pos_get_settings ( 'general ' , 'pos_only_products ' );
2828
2929 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 ' ) );
3131 add_filter ( 'woocommerce_variation_is_visible ' , array ( $ this , 'hide_pos_only_variations ' ), 10 , 4 );
3232 add_action ( 'woocommerce_store_api_validate_add_to_cart ' , array ( $ this , 'store_api_prevent_pos_only_add_to_cart ' ) );
3333
@@ -76,22 +76,26 @@ public function product_set_stock( WC_Product $product ): void {
7676 *
7777 * @return string
7878 */
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 () ) ) {
8382
8483 $ settings_instance = Settings::instance ();
8584 $ settings = $ settings_instance ->get_pos_only_product_visibility_settings ();
8685
8786 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 ) );
9197 }
9298 }
93-
94- return $ where ;
9599 }
96100
97101 /**
0 commit comments