is_main_query() ) { return; } $is_product_context = $query->is_search() || 'product' === $query->get( 'post_type' ) || ( function_exists( 'is_shop' ) && is_shop() ) || ( function_exists( 'is_product_category' ) && is_product_category() ); if ( ! $is_product_context ) { return; } $vars = apply_filters( 'ymmhero_query_vars', array( // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only pagination/filter parameter, sanitized at the point of use; no state is changed. 'make' => isset( $_GET['ymmhero_make'] ) ? sanitize_text_field( wp_unslash( $_GET['ymmhero_make'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only pagination/filter parameter, sanitized at the point of use; no state is changed. 'model' => isset( $_GET['ymmhero_model'] ) ? sanitize_text_field( wp_unslash( $_GET['ymmhero_model'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only pagination/filter parameter, sanitized at the point of use; no state is changed. 'year' => isset( $_GET['ymmhero_year'] ) ? absint( $_GET['ymmhero_year'] ) : 0, 'submodel' => null, ) ); if ( '' === $vars['make'] && '' === $vars['model'] && ! $vars['year'] ) { return; } $vehicle_ids = YMMHERO_DB::get_vehicle_ids_by_ymm( $vars['make'], $vars['model'], $vars['year'], $vars['submodel'] ); if ( empty( $vehicle_ids ) ) { $query->set( 'post__in', array( 0 ) ); return; } // The pivot answers this exactly; the previous meta_query matched 'i:42;' // as a substring of the serialized array, which is both slower and prone // to false positives across id boundaries. $product_ids = YMMHERO_DB::get_product_ids_by_vehicles( $vehicle_ids ); $existing = $query->get( 'post__in' ); if ( ! empty( $existing ) && is_array( $existing ) ) { $product_ids = array_intersect( $product_ids, array_map( 'absint', $existing ) ); } // post__in with an empty array is ignored by WP_Query and would show the // whole catalog, so an empty result must be forced with a sentinel id. $query->set( 'post__in', ! empty( $product_ids ) ? array_values( $product_ids ) : array( 0 ) ); } }