Good day.

There is a task to make a custom filter in the admin panel for products.

Products over 600,000 positions.

Code:

/** * Custom date filter */ if( isset( $_GET['parser_upload_date_from'] ) && $_GET['parser_upload_date_from'] || isset( $_GET['parser_upload_date_to'] ) && $_GET['parser_upload_date_to'] ) { $from = $_GET['parser_upload_date_from']? $_GET['parser_upload_date_from']: '2010-01-01'; $to = $_GET['parser_upload_date_to']? $_GET['parser_upload_date_to']: '3000-01-01'; // Get valid posts for date products $products = $wpdb->get_col( "select `post_id` from `sfp_products_meta` where str_to_date(`created_at`,'%Y-%m-%d') between '{$from}' and '{$to}' " ); if( ! $products ) { $products = array(0); } // Set query $query->query_vars['post__in'] = $products; } 

Now WordPress can not cope with the load and the server crashes.

Are there any ideas how to optimize this filter?

    1 answer 1

    Understood)

    Code:

     /** * Custom date filter */ if( isset( $_GET['parser_upload_date_from'] ) && $_GET['parser_upload_date_from'] || isset( $_GET['parser_upload_date_to'] ) && $_GET['parser_upload_date_to'] ) { // Get valid for date products add_filter( 'posts_where', function ( $where, \WP_Query $q ) { $from = $_GET['parser_upload_date_from']? $_GET['parser_upload_date_from']: '2010-01-01'; $to = $_GET['parser_upload_date_to']? $_GET['parser_upload_date_to']: '3000-01-01'; $where .= " AND STR_TO_DATE(wp_posts.post_date,'%Y-%m-%d') BETWEEN '{$from}' AND '{$to}'"; return $where; }, 10, 2 ); }