Hi @naastech2019
Please also try the below snippet (How to add custom code?):
add_action( 'pre_get_posts', 'woopt_pre_get_posts', 99 );
function woopt_pre_get_posts( $query ) {
if ( is_admin() || ( is_singular() && $query->is_main_query() ) ) {
return $query;
}
$post_type = $query->get( 'post_type' );
if ( ( $post_type === 'product' ) || ( is_array( $post_type ) && in_array( 'product', $post_type ) ) ) {
global $wpdb;
$exclude_ids = $query->get( 'post__not_in' );
$products = $wpdb->get_results( "SELECT id FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish' LIMIT 500", OBJECT );
if ( count( $products ) > 0 ) {
foreach ( $products as $pr ) {
$visible = WPCleverWoopt::woopt_is_visible( true, $pr->id );
if ( ! $visible ) {
// push to old values
array_push( $exclude_ids, $pr->id );
}
}
$query->set( 'post__not_in', $exclude_ids );
}
}
return $query;
}